我的程序編譯,但是當我運行它時,它給了我一個IndexOutOfBoundsException
。我想知道它有什麼問題,因爲我無法看到它。我的程序應該由用戶輸入並將其添加到ArrayList。如果我的錯誤對你顯而易見,我很抱歉,但我在使用ArrayLists方面相對較新。謝謝!我的ArrayList究竟出了什麼問題?
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」添加任何東西。你期望'arr.get(i)'在一個空的'List'上調用時怎麼做? – azurefrog 2015-04-02 21:59:25
爲什麼你想要一個嵌套數組列表? – 2015-04-02 21:59:27
你想創建一個沒有負數的數組列表嗎? – Arlind 2015-04-02 22:01:27