0

I have the following lists of Integer

whatToRemove = {2, 3, 8};

and another list

lst = {4, 6, 8}

I want to remove all whatToRemove the elements from lst.

I am trying to use lst.remove(whatToRemove.get(i)), but it is trying to remove the index, and not the value.

How can I do that?

ajduke
  • 4,991
  • 7
  • 36
  • 56
Tom Avni
  • 109
  • 3
  • 9
  • try list.removeAll(whatToRemove); – L.Butz May 18 '13 at 15:43
  • @jeppi answered it. Btw, I don't how, but it appears as if in your case `whatToRemove.get(i)` is treated as an `int`, that's why it is used as an index. If you put that piece within a `Integer.valueOf(...)` then it should work. – janos May 18 '13 at 16:01

8 Answers8

1
    List<Integer> whatToRemove = Arrays.asList(2, 3, 8);
    List<Integer> lst = new ArrayList<Integer>(Arrays.asList(4, 6, 8));
    lst.removeAll(whatToRemove);
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
0

You can either use removeAll(), which takes an object that implements the Collection interface as the parameter like this:

list.removeAll(collection);

Or you need do some sort of iteration in order to go through all of the values as you want to remove as remove() doesn't take another list as a parameter, only an index or an object:

for(int i=0;i<whatToRemove.size();i++){
lst.remove(whatToRemove.get(i));
}
0x6C38
  • 6,796
  • 4
  • 35
  • 47
0

Use iterator:

for (Iterator it=lst.iterator(); it.hasNext();) {
   if (whatToRemove.contains(it.next())
      it.remove();
}
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

if you are using Java collections to do this, you can simply call removeAll(Collection<T> o) on your first list to remove all occurences.

it would look like lst.removeAll(whatToRemove);

Alternatively you can iterate over all elements easily

for(int num : whatToRemove)
{
     lst.remove(num);
}

Both are viable, if you are using arrays, you can still use the enhanced for to iterate. but removing is a different problem.

Link to Javadoc: List

Tich
  • 85
  • 1
  • 6
0

This is because a List has a remove that removes an element by index and a remove that removes by object see:

http://docs.oracle.com/javase/6/docs/api/java/util/List.html#remove(int)

See how there are 2 removes :-(?

Unfortunately as your list contains integers Java thinks that you mean remove( item at) and not remove(object)

Use

Collection lst = [whatever]
lst.remove(whatToRemove.get(i))

...and then the remove should work correctly as Collection only has 1 kind of remove function (the one you want to use)

http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html

andy boot
  • 11,355
  • 3
  • 53
  • 66
0

Use the removeAll method on List

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0

With the list element you are going to want to remove the index of the objects within the list. For your example it will be integers obviously. To accomplish this you can used a for loop or pull each indexed object out in a manual way.

for loop - example :

for(int i = 0; i < whatToRemove.size(); i++){
     whatToRemove.remove(i);
 }

manual - example :

whatToRemove.remove(0);
whatToRemove.remove(1);
whatToRemove.remove(2);

This is from what I am understanding from your question that you would like to remove all the elements from your list call "whatToRemove".

Here is a link that may provide some insight into the list parameters Link - Java Object 'List'

RedRumming
  • 121
  • 4
0

you can use removeAll(Collection c) method as shown below

lst.removeAll(whatToRemove);

Please check the sample below

List<Integer> whatToRemove = new ArrayList<Integer>();
whatToRemove.add(new Integer(2));
whatToRemove.add(new Integer(3));
whatToRemove.add(new Integer(8));

List<Integer> lst = new ArrayList<Integer>();
lst.add(new Integer(4));
lst.add(new Integer(6));
lst.add(new Integer(8));

lst.removeAll(whatToRemove);

you can refer to below URL for more details on collection and removeAll method

http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html

S4beR
  • 1,872
  • 1
  • 17
  • 33