java '(' 或 '[' 应为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4302659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:45:34  来源:igfitidea点击:

'(' or '[' Expected

javaarrays

提问by Chris

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.

谢谢你。

回答by Yuval Adam

You're missing parenthesis on the constructor:

您在构造函数上缺少括号:

ArrayList accounts = new ArrayList();

回答by RoflcoptrException

Your constructor is wrong. It has to be;

你的构造函数是错误的。它一定要是;

ArrayList accounts = new ArrayList();

回答by Buhake Sindi

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

如果您使用的是 Java 5 及更高版本,您将看到ArrayList使用了generics

You can essentially to this:

您基本上可以这样做:

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

回答by Lukas Eder

Well, you can't construct an ArrayListlike this. Try

好吧,你不能构造ArrayList这样的。尝试

new ArrayList()

instead

反而

回答by daveb

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.

因为这样编译器将阻止您向其中添加整数(例如)而不是字符串。