【发布时间】:2015-02-09 01:14:26
【问题描述】:
运行以下代码时,出现错误:
ArrayList 中的add(java.lang.Integer) 不能应用于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<Integer>与int[]相同?你读过ArrayList#add的javadoc吗? -
注意:您的代码存在此问题未涵盖的其他问题。我不认为它会给你你期望的结果,即使在解决了 this 问题之后。