【发布时间】:2015-06-08 00:13:21
【问题描述】:
我的程序可以编译,但是当我运行它时,它给了我一个IndexOutOfBoundsException。我想知道它有什么问题,因为我看不到它。我的程序应该接受用户的输入并将其添加到 ArrayList。如果我的错误对您来说很明显,我很抱歉,但我在使用 ArrayLists 方面相对较新。谢谢!
ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
int counter = 0;
int i = 0;
Scanner in = new Scanner(System.in);
int input = in.nextInt();
while(i < 5)
{
input = in.nextInt();
if(input != 0){
arr.get(i).set(counter++, input);
}
else{
arr.get(i).set(counter++, input);
i++;
counter = 0;
}
}
System.out.println(arr);
【问题讨论】:
-
您永远不会向
arr添加任何内容。当调用空的List时,您希望arr.get(i)做什么? -
为什么要嵌套数组列表?
-
你想创建一个没有负数的数组列表吗?
标签: java arraylist indexoutofboundsexception