Help removing items from an ArrayList

I have two arraylists and i want to remove any elements that are in list1 and list 2 from list2.

I have this for code

for(int i = 0; i < list1.size(); i++){

list2.remove(list1.get(i));

}

for(int i = 0; i < list2.size(); i++){

if(list1.contains(list2.get(i))){

System.out.println(list1.indexOf(list2.get(i)));

}

}

When i run this code it prints out 5 numbers. If i run the first for loop twice i only get 3 numbers. If i run the first for loop 5 times i still get 3 numbers. Can someone please help me find out why some elements are not being removed.

[947 byte] By [andyrobo60a] at [2007-11-26 12:21:32]
# 1
What is the point of the first loop? You only need the second one.
CaptainMorgan08a at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 2
I need to remove the elements that are in both lists from list2 for later in the program. When i was getting strange results I put the second loop in to see if some of the elements are not being removed.
andyrobo60a at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 3
The get() method returns the Object at that index. Youre using it incorrectly.
CaptainMorgan08a at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 4

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 5
What is in each List? It's not obvious to me why you're having problems, the first loop should work.
kablaira at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 6
> What is the point of the first loop? You only need> the second one.Quite nonsensical considering the second one doesn't actually remove anything which is the stated requirement.
kablaira at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 7

> The get() method returns the Object at that index.

> Youre using it incorrectly.

Where? I don't see any place that "get" is being used incorrectly. It's a simple iteration using a common idiom and an attempt to remove it (if it exists) from the second list using List.remove(Object).

One potential cause of this kind of thing would be an improperly implemented equals() method in the elements or concurrent modification of the List.

kablaira at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 8

for(int i = 0; i < list1.size(); i++){

list2.remove(list1.get(i));

}

Im pretty sure that wouldn't work. It wouldn't even run unless the ArrayLists are holding ints.

CaptainMorgan08a at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 9

> Im pretty sure that wouldn't work. It wouldn't even

> run unless the ArrayLists are holding ints.

remove

boolean remove(Object o)

Removes the first occurrence in this list of the specified element (optional operation). If this 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).

Specified by:

remove in interface Collection<E>

Parameters:

o - element to be removed from this list, if present.

Returns:

true if this list contained the specified element.

Throws:

ClassCastException - if the type of the specified element is incompatible with this list (optional).

NullPointerException - if the specified element is null and this list does not support null elements (optional).

UnsupportedOperationException - if the remove method is not supported by this list.

kablaira at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 10

get

public E get(int index)

Returns the element at the specified position in this list.

Specified by:

get in interface List<E>

Specified by:

get in class AbstractList<E>

Parameters:

index - index of element to return.

Returns:

the element at the specified position in this list.

Throws:

IndexOutOfBoundsException - if index is out of range (index < 0 || index >= size()).

I think hes trying to use this method incorrectly.

CaptainMorgan08a at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 11
That's very nice, how do you think he's using it wrong? It's an idiom so common anyone who's been using collections for more than a day should be familiar with it.
kablaira at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 12
> That's very nice, how do you think he's using> it wrong?I dont know. I guess I was having a brainfart. Didnt think about the remove() method that takes an Object (even though you showed it to me). Sorry about that.
CaptainMorgan08a at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 13

If you look at the java doc of ArrayList.remove(Object o) method it says

-

public boolean remove(Object o)Removes a single instance of the specified element from this list, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if the list contains one or more such elements. Returns true if the list contained the specified element (or equivalently, if the list changed as a result of the call).

-

That means if you got duplicates of the element that you are trying to remove the remove call will remove only one of them.

Try changing your code to

for (int i=0; i<list1.size(); i++){

Object o = list1.get(i);

while(list2.remove(o));

}

>

LRMKa at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 14
The easiest thing to do is probably:list2.removeAll(list1);But at least the above post explains why you were getting the results you were.
kablaira at 2007-7-7 15:13:24 > top of Java-index,Archived Forums,Socket Programming...
# 15

Oops I just forgot about removeAll method.

I think the removeAll is the way to go not only becouse it is the simplast but also it will give better performance in long lists.

if you do remove in a loop every iteration will start the search again from the begining. But the removeAll will search through the list only once.

LRMKa at 2007-7-7 15:13:26 > top of Java-index,Archived Forums,Socket Programming...