Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 06-19-08   #1 (permalink)
Programmer
 
LyokoHaCk's Avatar
 
amd nvidia

Join Date: Aug 2006
Location: United States Inc.
Posts: 1,995

Rep: 74 LyokoHaCk is acknowledged by some
Unique Rep: 69
FAQs Submitted: 1
Folding Team Rank: 1894
Trader Rating: 2
Default Java Iterator Issue

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.
LyokoHaCk is offline I fold for Overclock.net   Reply With Quote
Old 06-19-08   #2 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,950
Blog Entries: 1

Rep: 558 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 333
FAQs Submitted: 6
Trader Rating: 5
Default

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;
	}
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw

Last edited by rabidgnome229 : 06-19-08 at 08:32 PM.
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 06-19-08   #3 (permalink)
Programmer
 
LyokoHaCk's Avatar
 
amd nvidia

Join Date: Aug 2006
Location: United States Inc.
Posts: 1,995

Rep: 74 LyokoHaCk is acknowledged by some
Unique Rep: 69
FAQs Submitted: 1
Folding Team Rank: 1894
Trader Rating: 2
Default

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?
LyokoHaCk is offline I fold for Overclock.net   Reply With Quote
Old 06-19-08   #4 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,950
Blog Entries: 1

Rep: 558 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 333
FAQs Submitted: 6
Trader Rating: 5
Default

Yup
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-02-08   #5 (permalink)
*cough* Stock *cough*
 
intel nvidia

Join Date: Jun 2008
Posts: 11

Rep: 1 stphung Unknown
Unique Rep: 1
Trader Rating: 0
Default

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>

System: Inspiron E1705
CPU
Intel Mobile Core 2 Duo T7200
Motherboard
Dell 0YD479
Memory
256Mb
Graphics Card
NVIDIA GeForce Go 7900 GS
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP
stphung is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 06:33 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.13930 seconds with 9 queries