This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes LinkedList List checkForComodification error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "LinkedList List checkForComodification error" Watch "LinkedList List checkForComodification error" New topic
Author

LinkedList List checkForComodification error

Matt Millar
Greenhorn

Joined: Oct 16, 2012
Posts: 11
Hello, I have an error that I have no idea how to fix or even what is going wrong here. I can only imagine that something is trying to be added or removed wronly from the linked list while im looping though it. Btu it does not seam like its trying to add things to the list while it running when i debug and print out actions it looks ok to me. Maybe im the switch staement is bad or something.
So this is what i have.
This is the real meat of the program that runs the processes in the linked list and calculates the service time and the wait time and moves them to a higher priority list if needed



I have this method for all for linked list so its pretty much the same for all just the last switch statement is different.


and here is where i run the methods calling them depending on robin starts out as one and the switch controlling that seams to work fine too


Thanks for any help with this
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

while iterating a List you cant remove an element using list reference . use iterator for that . i.e, you can use Iterator for loop instead of compiler syntactic sugar for each loop.
Matt Millar
Greenhorn

Joined: Oct 16, 2012
Posts: 11
so your saying something like this?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Matt Millar wrote:so your saying something like this?


No. You have to use the current Iterator. That's not exposed using a foreach loop, so you have to use an explicit Iterator. And if you want to add during iteration, you have to use an explicit ListIterator.

In the above, you're creating a new Iterator that's positioned before the beginning of the List and has no idea about your current iteration.
Matt Millar
Greenhorn

Joined: Oct 16, 2012
Posts: 11
I Think this is how to do it but it still gives me the error.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You need to use the Iterator's remove method. Not the List's. That's the whole point of it.

During an iteration, the collection may only be structurally modified through the Iterator that's doing the iteration. That means that if you try to use the collection's methods, or if another Iterator tries to use its methods, you'll get an error. A natural consequence of this is that if we have two or more iterations going on at once, none of them can modify the collection, since that would violate the rules for all the others.
Matt Millar
Greenhorn

Joined: Oct 16, 2012
Posts: 11
ok so using the new MyProcess mp will not remove it from the linked list.
now I see thanks I got a bit confused why I could not just remove it using the linked list way but now i see thanks again
here is the solution that works pretty well
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LinkedList List checkForComodification error
 
Similar Threads
Gradebook
Scjp Queries
3 questions regarding JPanel.
Adjectives for "variable" - automatic/local/member/method/method level/instance/class
Time in a program issues