How to specify which method Java use?

integer, java, list

Solution

This is a problem related to autoboxing. Actually, when `T == Integer`, you have two remove methods:

- `void remove(int index)`

- `void remove(Integer object)`

Just force the compiler to choose the appropriate, object based, version by casting it to an `Integer` or by using directly an `Integer`:

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

Problem

Now I have an `ArrayList<Integer>`. As this says, there are two remove methods for ArrayList. Suppose I have an integer 2, I want to remove the ELEMENT 2 in that list rather than the element on POSITION 2(third element), how should I tell Java to do so?

Original source