我正在尝试运行我发现在 StackOverflow 上标记为正确的以下代码:
List<Integer> intList = new ArrayList<Integer>();
for (int index = 0; index < ints.length; index++)
{
intList.add(ints[index]);
}
当我运行代码时出现错误:标记“;”上的语法错误,{ expected after this token on the line starting with List
有什么我想念的吗?
最佳答案
您可能已将此代码块放在类的顶层。它必须进入一个函数:
class Foo {
public static void main(String[] args) {
int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>();
for (int index = 0; index < ints.length; index++)
{
intList.add(ints[index]);
}
}
}
关于java - 新的 ArrayList<Integer>();在 Java 中给出语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065730/