2013-04-08 48 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

相关问题