当在运行下面的代码,我得到的错误:ArrayList的错误使用泛型类型
add(java.lang.Integer) in ArrayList cannot be applied to java.lang.Integer[]
如果我不ArrayList中使用泛型类型,它运行得很好。我不明白这个错误,因为arrayList和数组都是整数。我错过了什么?谢谢!
ArrayList<Integer> recyclingCrates = new ArrayList<Integer>();
int houses[] = new int[8];
int sum = 0;
for (int x = 0; x < 8; x++) {
System.out.println("How many recycling crates were set out at house " + x + "?");
houses[x] = scanner.nextInt();
for (Integer n : recyclingCrates){
houses[x]=n;
}
}
recyclingCrates.add(houses); //this is where I get the error
所以你认为'int []'和'Integer'是一样的吗?或者你认为'ArrayList'与'int []'是一样的吗?你读过'ArrayList#add'的javadoc吗? –
2015-02-09 01:15:20
注意:您的代码存在此问题未涉及的其他问题。我认为它不会给你你期望的结果,即使在解决这个问题之后。 – immibis 2015-02-09 01:16:55