为什么我的 ArrayList.remove(id) 调用不起作用?
我有一个 ArrayList,其中包含许多我要删除的项目。我有要删除的项目的ID存储在另一个列表中。图示以下代码应该可以正常工作,但由于某种原因,remove() 调用返回一个 false 值:
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() 调用也返回了一个 false 值,我会得到它,但它实际上返回了有问题的对象。我在这里错过了什么?
上述代码的输出:
Size before removing: 3
Removing id: 2:
javax.swing.JCheckBox[...]
false
Size after removing: 3