131

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.

1

13 Answers 13

193

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

22

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

1 Comment

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

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

list.remove(new Integer(2));

2 Comments

You should prefer Integer.valueOf over the constructor because it uses an internal cache to prevent unnecessary object creation.
This constructor is deprecated in Java 17.
4

try this:

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

Comments

4

The easiest way is:

list.remove((Integer)5);

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

3 Comments

Hi :), this answer has already been answered. If you have something to add to it, please include it as well, else consider removing this.
This is already answered in here
This still creates an object.
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.

Comments

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

Comments

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

Comments

1

In your case, this should also work,

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

1 Comment

Knew this question has been answered already, this is just an another way to achieve the same.
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

Comments

-1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.