2013-04-08 39 views
1

我想初始化一個像下面的字符串數組,但它有一個錯誤。爲什麼我不能在Java中像這樣構造字符串數組?

public class Account{ 
    private String[] account; 

    public Account() 
    { 
     account = {"A", "B", "C"}; 
    } 
} 

有沒有人知道它爲什麼不斷創建一個錯誤?

+1

您需要初始化數組 – 2013-04-08 06:24:56

+0

*「它有錯誤。」*複製/粘貼錯誤文本作爲[編輯問題](http://stackoverflow.com/posts/15872566/edit)並使用代碼格式。 – 2013-04-08 06:28:09

回答

8

正確的語法構造函數中的使用是

account = new String[]{"A", "B", "C"}; 

您嘗試使用快捷語法僅在聲明中對允許的:

private String[] account = {"A", "B", "C"}; 

至於爲什麼的區別見Why can array constants only be used in initializers?

+0

非常感謝。它真的幫助:) – jkl 2013-04-09 01:43:06

相關問題