New Posts  All Forums:Forum Nav:

Java Iterator Issue

post #1 of 5
Thread Starter 
Code:
public List<String> Pool;
public List<String> OtherList1;
public List<String> OtherList2;

public myClass() {
Pool = new ArrayList<String>();
OtherList1 = new ArrayList<String>();
OtherList2 = new ArrayList<String>();
} 
// Later, after adding some things to the lists...

public List<String> GPool() {
for(Iterator<String> i = Pool.iterator(); i.hasNext();) {
if(OtherList1.contains(i.next()) || OtherList2.contains(i.next())) { Pool.remove(i.next().toString()); }
}
return Pool;
}
When trying to access GPool, I get

Code:
 java.util.ConcurrentModificationException
[20:18:10.625] java.util.AbstractList$Itr.checkForComodification(Unknown Source)
[20:18:10.625] java.util.AbstractList$Itr.next(Unknown Source)
[20:18:10.625] GPool(myClass.java:70)
Line 70 is

Code:
if(OtherList1.contains(i.next()) || OtherList2.contains(i.next())) { Pool.remove(i.next().toString()); }
I am basically trying to check if the elements of two others lists are present in "Pool" and I'd like to remove them if they are, then return the list.
post #2 of 5
Each time you call i.next() it gives you a different object. So in line 70 you're looking 2 or 3 different objects in the iterator. Most likely you are overrunning the end of the list

Code:
public List<String> GPool() {
Iterator<String> i = Pool.iterator();
if(i.hasNext())
for(String next = i.next(); i.hasNext(); next = i.next()) {
if(OtherList1.contains(next) || OtherList2.contains(next())) { Pool.remove(next()); }
}
return Pool;
}

Edited by rabidgnome229 - 6/19/08 at 5:32pm
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #3 of 5
Thread Starter 
Quote:
Originally Posted by rabidgnome229 View Post
Each time you call i.next() it gives you a different object. So in line 70 you're looking 2 or 3 different objects in the iterator. Most likely you are overrunning the end of the list

Code:
public List<String> GPool() {
Iterator<String> i = Pool.iterator();
if(i.hasNext())
for(String next = i.next(); i.hasNext(); next = i.next()) {
if(OtherList1.contains(next) || OtherList2.contains(next())) { Pool.remove(next()); }
}
return Pool;
}
Thanks!
So storing it in a variable and calling it that way should solve it?
post #4 of 5
Yup
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #5 of 5
I would recommend using the new style for loop introduced in Java 5. Makes the code a lot more readable and you don't need to fuss with declaring an iterator, instead it is implicit.

<Begin Untested Code>
Code:
public List<String> GPool() {
    for ( String s : Pool )  {
        if ( OtherList1.contains(s) || OtherList2.contains(s) )
            Pool.remove(s);
    }
    return Pool;
}
</End Untested Code>
Inspiron E1705
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Mobile Core 2 Duo T7200 Dell 0YD479 NVIDIA GeForce Go 7900 GS 256Mb 
OS
Windows XP 
  hide details  
Reply
Inspiron E1705
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Mobile Core 2 Duo T7200 Dell 0YD479 NVIDIA GeForce Go 7900 GS 256Mb 
OS
Windows XP 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming