我試圖從用戶採取未知數量的整數(直到用戶輸入0),並計數每個輸入的整數。我想,在完成整數之後算數,我不得不將它們存儲在一個數組中。我做了一些研究並意識到創建一個長度未指定的Array的唯一方法是ArrayList是唯一的方法。但我的代碼,這部分顯示錯誤:無法使用Arraylist.get方法,它顯示「不兼容的操作數類型對象和int」
import java.util.Scanner;
import java.util.ArrayList;
public class IntegersOccurence {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the integers between 1 and 100: ");
ArrayList list = new ArrayList();
System.out.println(list.get(0));
//eclipse is showing an error in the line right below
while (list.get(list.size() - 1) != 0) {
list.add(input.nextInt());
}
}
}
也許你可以參考[這裏](https://stackoverflow.com/questions/18513308/what-is-the-difference-between-arraylist- arraylist-arraylistobject),因爲它可以回答你的問題:) – nkpg