2015-01-20 47 views
-5

我想設置對象e1的字符串[],但我不斷收到illegal start of expression錯誤。我是Java的新手,我完全不知道什麼是錯的。任何幫助表示讚賞。如何解決Java中非法表達式的啓動問題?

public class Employeetest { 

    public static void main(String[] args) { 

     Employee e1 = new Employee(); 
     e1.person ={"","","","",""}; 

     e1.printInfo(); 
    } 
} 


public class Employee { 

    String[] person ={"","","","",""}; 

    public void printInfo() { 
     System.out.println("Name:" + person[0]); 
     System.out.println("Gender:" + person[1]); 
     System.out.println("Job Title:" + person[2]); 
     System.out.println("Organization:" + person[3]); 
     System.out.println("Birthday:" + person[4]); 
    } 
} 
+0

歡迎來到Stack Overflow!你有什麼具體的錯誤?你覺得他們是什麼意思?到目前爲止你做了什麼來解決它?您提供的細節越多,我們就可以提供更好的幫助。 – templatetypedef 2015-01-21 00:01:34

+0

[Declare array in Java?]的可能重複(http://stackoverflow.com/questions/1200621/declare-array-in-java) – 2015-01-21 00:02:26

回答

3

當初始化字符串數組,你不必提供new String[],前提是你已經初始化的聲明,例如數組

String[] person ={"","","","",""}; 

在別的地方你改變了價值,你必須提供new String[]這樣的:

e1.person = new String[] {"","","","",""}; 

然而,在這裏,你不需要重新初始化person陣列。當您創建Employee對象時,person已按照您聲明的進行初始化。您可以刪除此行:

e1.person ={"","","","",""}; 

它會工作相同。但除了打印空字段之外,除非將初始化陣列的元素設置爲除""以外的內容,否則它不會有太多工作。

相關問題