开发者

'(' or '[' Expected

开发者 https://www.devze.com 2023-01-27 04:35 出处:网络
I am getting the following error when trying to compile my program. \'(\' or \'[\' Expected. public AccountArrayList()

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

精彩评论

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