betteroutthanin
betteroutthanin

Reputation: 7556

Java, how to remove an Integer item in an ArrayList

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

Answers (13)

cherry
cherry

Reputation: 356

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

Upvotes: -1

Tirumudi
Tirumudi

Reputation: 443

In your case, this should also work,

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

Upvotes: 1

harshcoder
harshcoder

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

Nika
Nika

Reputation: 1049

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

Upvotes: 0

wired00
wired00

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

Abdalrhman Alkhulaqi
Abdalrhman Alkhulaqi

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

Rakesh KR
Rakesh KR

Reputation: 6527

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

Upvotes: 1

user2575725
user2575725

Reputation:

try this:

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

Upvotes: 4

badoualy
badoualy

Reputation: 386

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

list.remove(new Integer(2));

Upvotes: 7

Rohit Jain
Rohit Jain

Reputation: 213271

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

Upvotes: 34

chm
chm

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

Evgeniy Dorofeev
Evgeniy Dorofeev

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

Ashish
Ashish

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

Related Questions