Java, how to remove an Integer item in an ArrayList

Java

Java Problem Overview


Suppose I have such an ArrayList:

ArrayList<Integer> list = new ArrayList<Integer>();

After the adding operation:

list.add(2);
list.add(3);
list.add(5);
list.add(7);

I want to remove number 2, if I do

list.remove(2);

then number 5 will be deleted, how could I delete number 2? And suppose I don't know the index of number 2.

Java Solutions


Solution 1 - Java

try this

list.removeAll(Arrays.asList(2));

it will remove all elements with value = 2

you can also use this

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

but it will remove only first occurence of 2

list.remove(2) does not work because it matches List.remove(int i) which removes element with the specified index

Solution 2 - Java

There are two versions of remove() method:

With an ArrayList<Integer>, removing an integer value like 2, is taken as index, as remove(int) is an exact match for this. It won't box 2 to Integer, and widen it.

A workaround is to get an Integer object explicitly, in which case widening would be prefered over unboxing:

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

Solution 3 - Java

instead of:

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

you can of course just use:

list.remove((Integer) 2);

This will cast to an Integer object rather than primitive and then remove() by Object instead of Arraylist Index

Solution 4 - Java

Solution 5 - Java

The easiest way is:

list.remove((Integer)5);

No unnecessary object creation, just casting the Index to Integer. BONUS: The simplest syntax.

Solution 6 - Java

try this:

list.remove(list.indexOf(2));

Solution 7 - Java

There's no explicit method for finding a particular list element and then removing it. You have to first find it with using the indexOf method:

int index = list.indexOf(element); // for your example element would be 2
list.remove(index);

Be aware that indexOf returns the index of the first occurrence of the object you give it, so you'll have to adjust accordingly for cases where you want to delete an item that is in the list multiple times.

Solution 8 - Java

Try,

list.remove(0);
  1. remove(int index)

    Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

  2. remove(Object o)

    Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Solution 9 - Java

Simply if you use method like this it will remove element at index 2

YOUR ARRAYLIST: 2,3,5,7

> list.remove(2);

OUTPUT: 2,5,7

And if you use method like this it will remove element with value 2

YOUR ARRAYLIST: 2,3,5,7

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

OUTPUT: 3,5,7

Hope it help...

Solution 10 - Java

You calling list.remove(int) method, but you need to call list.remove(Object) method.

There are several ways to do this:

  1. list.remove(Integer.valueOf(2)); // removes the first occurrence of 2
  2. list.remove(list.indexOf(2)); // also removes the first occurrence of 2
  3. list.removeAll(Arrays.asList(2)); // removes all occurrences of 2

Solution 11 - Java

In your case, this should also work,

list.removeIf(element -> element == 2);

Solution 12 - Java

you can use list.remove(new Integer(i)) where i is the element you want to remove.

Solution 13 - Java

list.remove(0);

0 represent element at index 0

and you have written list.remove(2); which means remove element at index 2 (i.e element at third place i.e 5 since ArrayList Start with index 0,1,2....)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbetteroutthaninView Question on Stackoverflow
Solution 1 - JavaEvgeniy DorofeevView Answer on Stackoverflow
Solution 2 - JavaRohit JainView Answer on Stackoverflow
Solution 3 - Javawired00View Answer on Stackoverflow
Solution 4 - JavabadoualyView Answer on Stackoverflow
Solution 5 - JavaharshcoderView Answer on Stackoverflow
Solution 6 - Javauser2575725View Answer on Stackoverflow
Solution 7 - JavachmView Answer on Stackoverflow
Solution 8 - JavaRakesh KRView Answer on Stackoverflow
Solution 9 - JavaAbdalrhman AlkhulaqiView Answer on Stackoverflow
Solution 10 - JavaLitvinov NikitaView Answer on Stackoverflow
Solution 11 - JavaTirumudiView Answer on Stackoverflow
Solution 12 - JavacherryView Answer on Stackoverflow
Solution 13 - JavaAshishView Answer on Stackoverflow