开发者

'(' or '[' Expected

I am getting the following error when trying to compile my program.

'(' or '[' Expected.

 public AccountArrayList()
{
    // line one below is the hi-lighted code
    ArrayList accounts = new ArrayList;
    accounts.add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.开发者_如何学编程add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.add("1");
    accounts.add(5,"900");
}

Thank you.


You're missing parenthesis on the constructor:

ArrayList accounts = new ArrayList();


Your constructor is wrong. It has to be;

ArrayList accounts = new ArrayList();


If you are using Java 5 and higher, you will see that ArrayList uses generics.

You can essentially to this:

ArrayList<String> accounts = new ArrayList<String>();


Well, you can't construct an ArrayList like this. Try

new ArrayList()

instead


It's the call to the constructor that's the problem, it should be

ArrayList accounts = new ArrayList();

Also, you would do well to specify it like this:

ArrayList<String> accounts = new ArrayList<String>();

Because then the compiler will prevent you from adding Integers to it (for example) rather than Strings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜