我有一個ArrayList,其中包括我想要刪除的項目數。我有要刪除項目的ID存儲在另一個列表中。想通下面的代碼應該平凡的工作,但由於某種原因,刪除()調用返回錯誤值:爲什麼我的ArrayList.remove(id)調用不起作用?
ArrayList<Integer> toRemove = new ArrayList<Integer>();
ArrayList<JCheckBox> al = new ArrayList<JCheckBox>();
/* Code that adds a bunch of items to al, and a few integers to toRemove */
System.out.println("Size before removing: " + al.size());
for (int i = toRemove.size() - 1; i >= 0; i--) {
System.out.println("Removing id: " + toRemove.get(i) + ": ");
System.out.println(al.get(toRemove.get(i)));
System.out.println(al.remove(toRemove.get(i)));
}
System.out.println("Size after removing: " + al.size());
我會得到它如果get()調用也返回了一個錯誤的值,但它確實返回有問題的對象。我在這裏錯過了什麼?
上述代碼的輸出:
Size before removing: 3
Removing id: 2:
javax.swing.JCheckBox[...]
false
Size after removing: 3
你能張貼'al'和'toRemove'的確切聲明嗎? – 2010-08-26 22:20:29
發佈了所需的定義。 – zigdon 2010-08-26 22:49:56