當在運行下面的代碼,我得到的錯誤: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