Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly removing an Integer from a List<Integer>

People also ask

How do I remove an integer from a list?

We can remove integers or digits from the list by using python function isinstance(). By using this method, we can get items or elements which does not satisfies the function condition. Here we apply if not condition to check whether the condition is satisfied or not.

How do you remove an integer from a list in Python?

In Python, use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

How do you delete an int in Java?

The remove(int index) method of List interface in Java is used to remove an element from the specified index from a List container and returns the element after removing it. It also shifts the elements after the removed element by 1 position to the left in the List.


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.


You can use casting

list.remove((int) n);

and

list.remove((Integer) n);

It doesn't matter if n is an int or Integer, the method will always call the one you expect.

Using (Integer) n or Integer.valueOf(n) is more efficient than new Integer(n) as the first two can use the Integer cache, whereas the later will always create an object.


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.


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).


Any educated guess on what happens when you execute list.remove(1)? What about list.remove(new Integer(1))?

There is no need to guess. The first case will result in List.remove(int) being called, and the element at position 1 will be removed. The second case will result in List.remove(Integer) being called, and the element whose value is equal to Integer(1) will be removed. In both cases, the Java compiler selects the closest matching overload.

Yes, there is potential for confusion (and bugs) here, but it is a fairly uncommon use-case.

When the two List.remove methods were defined in Java 1.2, the overloads were not ambiguous. The problem only arose with the introduction of generics and autoboxing in Java 1.5. In hind-sight, it would have been better if one of the remove methods had been given a different name. But it is too late now.


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;
  }
}

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.