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

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提到的-精确的参数匹配优先于自动装箱。


当前回答

list.remove(4)是list的精确匹配。Remove (int index),因此它将被调用。如果你想调用list.remove(Object),请执行以下操作:list.remove((Integer)4)。

其他回答

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

list.remove(int_parameter);

移除给定位置和的元素

list.remove(Integer_parameter);

从列表中移除给定对象。

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

这里有个诀窍。

让我们举两个例子:

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缓存,而后者总是创建一个对象。

list.remove(4)是list的精确匹配。Remove (int index),因此它将被调用。如果你想调用list.remove(Object),请执行以下操作:list.remove((Integer)4)。

当你执行list.remove(1)时会发生什么?列表呢?remove(新的整数(1))?

没有必要猜测。第一种情况将导致调用List.remove(int),位置1的元素将被移除。第二种情况将导致调用List.remove(Integer),值等于Integer(1)的元素将被删除。在这两种情况下,Java编译器都会选择最接近的匹配重载。

是的,这里有可能出现混乱(和bug),但这是一个相当不常见的用例。

当二名单。remove方法是在Java 1.2中定义的,重载是明确的。这个问题是在Java 1.5中引入泛型和自动装箱后才出现的。事后看来,如果其中一个删除方法被赋予不同的名称,效果会更好。但现在已经太晚了。