Reputation: 7556
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
.
Upvotes: 129
Views: 115666
Reputation: 356
you can use list.remove(new Integer(i))
where i is the element you want to remove.
Upvotes: -1
Reputation: 443
In your case, this should also work,
list.removeIf(element -> element == 2);
Upvotes: 1
Reputation: 59
The easiest way is:
list.remove((Integer)5);
No unnecessary object creation, just casting the Index to Integer. BONUS: The simplest syntax.
Upvotes: 4
Reputation: 1049
You calling list.remove(int)
method, but you need to call list.remove(Object)
method.
There are several ways to do this:
list.remove(Integer.valueOf(2)); // removes the first occurrence of 2
list.remove(list.indexOf(2)); // also removes the first occurrence of 2
list.removeAll(Arrays.asList(2)); // removes all occurrences of 2
Upvotes: 0
Reputation: 14438
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
Upvotes: 21
Reputation: 333
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...
Upvotes: 1
Reputation: 6527
Try,
list.remove(0);
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.
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).
Upvotes: 1
Reputation: 386
I think this is what you want : ArrayList <Integer> with the get/remove method
list.remove(new Integer(2));
Upvotes: 7
Reputation: 213271
There are two versions of remove()
method:
ArrayList#remove(Object)
that takes an Object
to remove, and ArrayList#remove(int)
that takes an index
to remove. 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));
Upvotes: 34
Reputation: 1519
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.
Upvotes: 2
Reputation: 136042
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
Upvotes: 190
Reputation: 1941
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....)
Upvotes: -3