我试图从用户采取未知数量的整数(直到用户输入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