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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3579981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:30:20  来源:igfitidea点击:

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

javadebuggingarraylist

提问by zigdon

I have an ArrayList, which includes a number of items I want to remove. I have the ids of the items to remove stored in another list. Figured the following code should work trivially, but for some reason, the remove() calls are returning a false value:

我有一个 ArrayList,其中包含许多我想删除的项目。我将要删除的项目的 ID 存储在另一个列表中。认为以下代码应该可以正常工作,但由于某种原因,remove() 调用返回了一个错误值:

  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. What am I missing here?

如果 get() 调用也返回一个假值,我会得到它,但它实际上返回了有问题的对象。我在这里错过了什么?

The output of the code above:

上面代码的输出:

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

回答by ILMTitan

My guess is you are having a problem with the fact that remove()is overloaded with both intand Object, while get()only takes an int. Try remove(toRemove.get(i).intValue()).

我的猜测是您有这样一个事实的问题remove()超载既intObject,而get()只需要一个int。试试remove(toRemove.get(i).intValue())

remove(Object)from AbstractCollectionwill search through the list and remove the given object, which won't be there because you are sending it an Integerand the list only has JCheckBoxs. You are trying to call remove(int), but because you are giving it an Integer, the Object overload is called instead. By converting the Integerto an int, you avoid this problem

remove(Object)fromAbstractCollection将搜索列表并删除给定的对象,该对象将不存在,因为您正在发送它Integer并且列表只有JCheckBoxs。您正在尝试调用remove(int),但是因为您给它一个Integer,所以会调用 Object 重载。通过将 the 转换Integer为 an int,可以避免此问题

Also, can you always be sure the Id in toRemove always equals the index? If toRemove is not in greatest to least order, it won't be.

另外,您能否始终确保 toRemove 中的 Id 始终等于索引?如果 toRemove 不是最大到最小顺序,则不会。

回答by dhm

There are two problems with your code. First, the wrong "toRemove" method is getting invoked. When you call "toRemove.get(i)", the return value is autoboxed into a java.lang.Integer, instead of an int. Therefore, java.util.List#remove(Object) is called instead of java.util.List#remove(int). It's trying to remove an Integer object, and returns false. If you cast the Integer to an int, the desired method will get invoked.

您的代码有两个问题。首先,调用了错误的“toRemove”方法。当您调用“toRemove.get(i)”时,返回值将自动装箱为 java.lang.Integer,而不是 int。因此,调用的是 java.util.List#remove(Object) 而不是 java.util.List#remove(int)。它试图删除一个 Integer 对象,并返回 false。如果您将 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.

第二个问题:每次删除列表中的一个元素时,所有后续元素的索引都会更改,因为这些元素被“向下移动”。有几种方法可以解决这个问题。一种是按降序对索引列表进行排序。另一种方法是使用一组索引,创建一个新数组,然后仅将索引不在该集合中的那些元素复制到新数组中。

回答by Hila's Master

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/