Properly removing an Integer from a List

2018-12-31 08:34发布

Here's a nice pitfall I just encountered. Consider a list of integers:

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

Any educated guess on what happens when you execute list.remove(1)? What about list.remove(new Integer(1))? This can cause some nasty bugs.

What is the proper way to differentiate between remove(int index), which removes an element from given index and remove(Object o), which removes an element by reference, when dealing with lists of integers?


The main point to consider here is the one @Nikita mentioned - exact parameter matching takes precedence over auto-boxing.

9条回答
呛了眼睛熬了心
2楼-- · 2018-12-31 08:54

Simply I did like following as suggested by #decitrig in accepted answer first comment.

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

This helped me. Thanks again #decitrig for your comment. It may help for some one.

查看更多
唯独是你
3楼-- · 2018-12-31 08:58

list.remove(4) is an exact match of list.remove(int index), so it will be called. If you want to call list.remove(Object) do the following: list.remove((Integer)4).

查看更多
旧人旧事旧时光
4楼-- · 2018-12-31 09:00

Java always calls the method that best suits your argument. Auto boxing and implicit upcasting is only performed if there's no method which can be called without casting / auto boxing.

The List interface specifies two remove methods (please note the naming of the arguments):

  • remove(Object o)
  • remove(int index)

That means that list.remove(1) removes the object at position 1 and remove(new Integer(1)) removes the first occurrence of the specified element from this list.

查看更多
萌妹纸的霸气范
5楼-- · 2018-12-31 09:07

I don't know about 'proper' way, but the way you suggested works just fine:

list.remove(int_parameter);

removes element at given position and

list.remove(Integer_parameter);

removes given object from the list.

It's because VM at first attempts to find method declared with exactly the same parameter type and only then tries autoboxing.

查看更多
后来的你喜欢了谁
6楼-- · 2018-12-31 09:09

Note that even if the VM did not do the right thing, which it does, you could still ensure proper behaviour by using the fact that remove(java.lang.Object) operates on arbitrary objects:

myList.remove(new Object() {
  @Override
  public boolean equals(Object other) {
    int k = ((Integer) other).intValue();
    return k == 1;
  }
}
查看更多
只靠听说
7楼-- · 2018-12-31 09:09

Simple Java Code Explanation for visualizing difference between ArrayList.remove(Object o) and ArrayList.remove(int index)

CODE :

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

for (int a = 0; a < list.size(); a++) {
    System.out.print("" + list.get(a));
}

System.out.print("  ");

//CASE 1: We are removing data object 7.
list.remove(new Integer(7));

for (int a = 0; a < list.size(); a++) {
    System.out.print("" + list.get(a));
}

System.out.print("  ");

//CASE 2: Again we are removing data object 7.
list.remove(new Integer(7));

for (int a = 0; a < list.size(); a++) {
    System.out.print("" + list.get(a));
}

System.out.print("  ");

//CASE 3: We are removing data at index 1
list.remove(1);

for (int a = 0; a < list.size(); a++) {
    System.out.print("" + list.get(a));
}

OUTPUT :

1274567
124567
12456
1456

EXPLAINATION :

CASE 1: We are removing first occurring Integer object with data 7.

CASE 2: Then we are again doing same thing as in CASE 1 from remaining data.

CASE 3: We are removing data at index position 1.

CONCLUSION :

Taking an example of ArrayList<Integer> as used in above java code: ArrayList.remove(Integer object) will simply remove the specific Integer object occurring first in the ArrayList. However, ArrayList.remove(int index) will always remove the array element at given index position.

MORE ABOUT :

We cannot declare ArrayList<int> because it int is primary data-type. Any Class which has the base-class Object is only specified in the ArrayList generic datatype. eg: ArrayList<String>, ArrayList<Integer> etc.

查看更多
登录 后发表回答