这是我刚刚遇到的一个陷阱。 考虑一个整数列表:

List<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(6);
list.add(7);
list.add(1);

当你执行list.remove(1)时会发生什么?列表呢?remove(新的整数(1))?这可能会导致一些讨厌的bug。

在处理整数列表时,正确区分remove(int index)和remove(Object o)的方法是什么?前者从给定的索引中删除一个元素,后者通过引用删除一个元素。


这里要考虑的要点是@Nikita提到的-精确的参数匹配优先于自动装箱。


当前回答

这里有个诀窍。

让我们举两个例子:

public class ArrayListExample {

public static void main(String[] args) {
    Collection<Integer> collection = new ArrayList<>();
    List<Integer> arrayList = new ArrayList<>();

    collection.add(1);
    collection.add(2);
    collection.add(3);
    collection.add(null);
    collection.add(4);
    collection.add(null);
    System.out.println("Collection" + collection);

    arrayList.add(1);
    arrayList.add(2);
    arrayList.add(3);
    arrayList.add(null);
    arrayList.add(4);
    arrayList.add(null);
    System.out.println("ArrayList" + arrayList);

    collection.remove(3);
    arrayList.remove(3);
    System.out.println("");
    System.out.println("After Removal of '3' :");
    System.out.println("Collection" + collection);
    System.out.println("ArrayList" + arrayList);

    collection.remove(null);
    arrayList.remove(null);
    System.out.println("");
    System.out.println("After Removal of 'null': ");
    System.out.println("Collection" + collection);
    System.out.println("ArrayList" + arrayList);

  }

}

现在让我们看一下输出:

Collection[1, 2, 3, null, 4, null]
ArrayList[1, 2, 3, null, 4, null]

After Removal of '3' :
Collection[1, 2, null, 4, null]
ArrayList[1, 2, 3, 4, null]

After Removal of 'null': 
Collection[1, 2, 4, null]
ArrayList[1, 2, 3, 4]

现在让我们分析一下输出:

当3从集合中移除时,它调用集合的remove()方法,该方法以Object o为参数。因此它删除了对象3。 但在arrayList对象中,它被索引3覆盖,因此第4个元素被删除。 在第二个输出中,通过相同的对象删除逻辑,在两种情况下都删除null。

为了移除数字3这个对象我们需要显式地将3作为一个对象传递。

这可以通过使用包装器类Integer进行强制转换或包装来实现。

Eg:

Integer removeIndex = Integer.valueOf("3");
collection.remove(removeIndex);

其他回答

我不知道“合适”的方式,但你建议的方式很好:

list.remove(int_parameter);

移除给定位置和的元素

list.remove(Integer_parameter);

从列表中移除给定对象。

这是因为VM首先试图找到声明了完全相同参数类型的方法,然后才尝试自动装箱。

简单地说,我喜欢遵循#decitrig在接受的回答第一条评论中的建议。

list.remove(Integer.valueOf(intereger_parameter));

这对我很有帮助。再次感谢#decitrig的评论。它可能对某些人有帮助。

这里有个诀窍。

让我们举两个例子:

public class ArrayListExample {

public static void main(String[] args) {
    Collection<Integer> collection = new ArrayList<>();
    List<Integer> arrayList = new ArrayList<>();

    collection.add(1);
    collection.add(2);
    collection.add(3);
    collection.add(null);
    collection.add(4);
    collection.add(null);
    System.out.println("Collection" + collection);

    arrayList.add(1);
    arrayList.add(2);
    arrayList.add(3);
    arrayList.add(null);
    arrayList.add(4);
    arrayList.add(null);
    System.out.println("ArrayList" + arrayList);

    collection.remove(3);
    arrayList.remove(3);
    System.out.println("");
    System.out.println("After Removal of '3' :");
    System.out.println("Collection" + collection);
    System.out.println("ArrayList" + arrayList);

    collection.remove(null);
    arrayList.remove(null);
    System.out.println("");
    System.out.println("After Removal of 'null': ");
    System.out.println("Collection" + collection);
    System.out.println("ArrayList" + arrayList);

  }

}

现在让我们看一下输出:

Collection[1, 2, 3, null, 4, null]
ArrayList[1, 2, 3, null, 4, null]

After Removal of '3' :
Collection[1, 2, null, 4, null]
ArrayList[1, 2, 3, 4, null]

After Removal of 'null': 
Collection[1, 2, 4, null]
ArrayList[1, 2, 3, 4]

现在让我们分析一下输出:

当3从集合中移除时,它调用集合的remove()方法,该方法以Object o为参数。因此它删除了对象3。 但在arrayList对象中,它被索引3覆盖,因此第4个元素被删除。 在第二个输出中,通过相同的对象删除逻辑,在两种情况下都删除null。

为了移除数字3这个对象我们需要显式地将3作为一个对象传递。

这可以通过使用包装器类Integer进行强制转换或包装来实现。

Eg:

Integer removeIndex = Integer.valueOf("3");
collection.remove(removeIndex);

你可以使用类型转换

list.remove((int) n);

and

list.remove((Integer) n);

不管n是int还是Integer,该方法总是会调用您所期望的方法。

使用(Integer) n或Integer. valueof (n)比新的Integer(n)更有效,因为前两者可以使用Integer缓存,而后者总是创建一个对象。

Java总是调用最适合你的参数的方法。自动装箱和隐式上强制转换只在没有可以调用的方法时执行。

List接口指定了两个remove方法(请注意参数的命名):

删除(对象) 删除(int)

这意味着list.remove(1)删除位置1的对象,remove(new Integer(1))从列表中删除第一次出现的指定元素。