Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedList List checkForComodification error

 
Matt Millar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so your saying something like this?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Think this is how to do it but it still gives me the error.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic