121

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.

betteroutthanin
  • 7,148
  • 8
  • 29
  • 48
  • 1
    Possible duplicate of [Properly removing an Integer from a List](http://stackoverflow.com/questions/4534146/properly-removing-an-integer-from-a-listinteger) – Sayed Abolfazl Fatemi Feb 27 '17 at 11:16

13 Answers13

183

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

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
34

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));
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
19

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

wired00
  • 13,930
  • 7
  • 70
  • 73
  • So it will look for the object as long as it is an Integer and not an int right? For example- public void findById(Integer id){ Object item = array.get(id); } will return the value and not the position correct? – dbrewster Mar 01 '21 at 20:57
7

I think this is what you want : ArrayList <Integer> with the get/remove method

list.remove(new Integer(2));
Community
  • 1
  • 1
badoualy
  • 386
  • 2
  • 10
4

try this:

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

The easiest way is:

list.remove((Integer)5);

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

harshcoder
  • 51
  • 1
  • 3
2

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.

chm
  • 1,519
  • 13
  • 21
1

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

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
1

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

0

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Litva Nick
  • 483
  • 8
  • 11
0

In your case, this should also work,

list.removeIf(element -> element == 2);
Tirumudi
  • 423
  • 1
  • 4
  • 18
-1

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

cherry
  • 336
  • 1
  • 2
  • 9
-3
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....)

Ashish
  • 1,943
  • 2
  • 14
  • 17