简体   繁体   English

为什么我的ArrayList.remove(id)调用不起作用?

[英]Why is my ArrayList.remove(id) call not working?

I have an ArrayList, which includes a number of items I want to remove. 我有一个ArrayList,其中包含我想删除的一些项目。 I have the ids of the items to remove stored in another list. 我有要删除的项目的ID存储在另一个列表中。 Figured the following code should work trivially, but for some reason, the remove() calls are returning a false value: 认为下面的代码应该是平常工作的,但由于某种原因,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());

I'd get it if the get() call also returned a false value, but it does actually return the object in question. 如果get()调用也返回了false值,我会得到它,但它实际上会返回有问题的对象。 What am I missing here? 我在这里错过了什么?

The output of the code above: 上面代码的输出:

Size before removing: 3
Removing id: 2: 
javax.swing.JCheckBox[...]
false
Size after removing: 3

My guess is you are having a problem with the fact that remove() is overloaded with both int and Object , while get() only takes an int . 我的猜测是你遇到的问题是remove()是用intObject重载的,而get()只是一个int Try remove(toRemove.get(i).intValue()) . 尝试remove(toRemove.get(i).intValue())

remove(Object) from AbstractCollection will search through the list and remove the given object, which won't be there because you are sending it an Integer and the list only has JCheckBox s. AbstractCollection remove(Object)将搜索列表并删除给定的对象,该对象不会出现,因为您发送的是Integer且列表中只有JCheckBox You are trying to call remove(int) , but because you are giving it an Integer , the Object overload is called instead. 您正在尝试调用remove(int) ,但因为您给它一个Integer ,所以会调用Object重载。 By converting the Integer to an int , you avoid this problem 通过将Integer转换为int ,可以避免此问题

Also, can you always be sure the Id in toRemove always equals the index? 此外,您是否可以始终确保toRemove中的Id始终等于索引? If toRemove is not in greatest to least order, it won't be. 如果toRemove不是最大到最小的顺序,它就不会。

There are two problems with your code. 您的代码有两个问题。 First, the wrong "toRemove" method is getting invoked. 首先,调用错误的“toRemove”方法。 When you call "toRemove.get(i)", the return value is autoboxed into a java.lang.Integer, instead of an int. 当您调用“toRemove.get(i)”时,返回值将自动装入java.lang.Integer,而不是int。 Therefore, java.util.List#remove(Object) is called instead of java.util.List#remove(int). 因此,调用java.util.List #remove(Object)而不是java.util.List #remove(int)。 It's trying to remove an Integer object, and returns false. 它试图删除一个Integer对象,并返回false。 If you cast the Integer to an int, the desired method will get invoked. 如果将Integer强制转换为int,则将调用所需的方法。

Second problem: each time you remove an element of the list, the indexes of all subsequent elements change, as those elements are "shifted" down. 第二个问题:每次删除列表中的元素时,所有后续元素的索引都会发生变化,因为这些元素会“向下移动”。 There are several ways to get around this. 有几种方法可以解决这个问题。 One is to sort your list of indexes in descending order. 一种是按降序对索引列表进行排序。 Another would be to use a set of indexes, create a new array, and copy into the new array only those elements whose index is not in the set. 另一种方法是使用一组索引,创建一个新数组,并将那些索引不在集合中的元素复制到新数组中。

javax.swing.JCheckBox[...]? javax.swing.JCheckBox中[...]

Also, try remove elements by Objects value, like here: http://www.easywayserver.com/blog/java-remove-element-in-arraylist/ 另外,尝试按对象删除元素值,如下所示: http//www.easywayserver.com/blog/java-remove-element-in-arraylist/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM