IAmBlake IAmBlake - 1 year ago 74
Java Question

How to differentiate between ArrayList.remove(position) and ArrayList.remove(value)?

If I take an ArrayList of String type object like


and if i try to remove 'C' from the list then i call ArrayList.remove('C') and again if i try to remove element of index 2 then i call ArrayList.remove(2).

But if my ArrayList is a list of integer type objects like


then if i want to remove 2 and the element of index 1 then how can it differentiate between these to method ArrayList.remove(2)(value) and ArrayList.remove(1)(index)(as both can remove 1,2 from the list to make it {3,4} or both can remove element in that index 2,3 to make it {1,4})?

Answer Source

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

It will remove the first occurence of 2.