|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Java Iterator Issue
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Programmer
|
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;
}
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) Code:
if(OtherList1.contains(i.next()) || OtherList2.contains(i.next())) { Pool.remove(i.next().toString()); }
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
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;
}
Last edited by rabidgnome229 : 06-19-08 at 08:32 PM. |
|||||||||||||
|
|
|
|
#3 (permalink) | |
|
Programmer
|
Quote:
So storing it in a variable and calling it that way should solve it? |
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
Apple Doesn't Love You
|
Yup
|
|||||||||||||
|
|
|
|
#5 (permalink) | ||||||||
|
*cough* Stock *cough*
|
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;
}
__________________
Member of the OCN Diablo III Club
|
||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|