• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cast problem  RSS feed

 
mike ickx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


if i try to use this code i get this error : java.util.ConcurrentModificationException

can someone please explain why this cast isnt working ??

the DTO contains two integers, mandje = Collection /ArrayList, bestellijn is a DTO


[HENRY: Added Code Tags]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i try to use this code i get this error : java.util.ConcurrentModificationException


While iterating through a collection, you are not allowed to change the structure of the collection (unless you use the iterator's methods to do so). Here you are adding more items to the collection, while you are still iterating through the collection.

can someone please explain why this cast isnt working ??


What cast?

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And BTW, it looks like the objects in the collection are mutable, and you change them while iterating. This part is fine. But why do you need to add another copy, when you change it? (this part is not fine and causing the exception)

Henry
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh, that was my exact same question the first time I messed with Iterators. In a regular for loop you are allowed to modify your iterator any way you'd like but when iterating over a collection using an Iterator you must not change the structure of the collection as Henry already stated.
 
mike ickx
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
And BTW, it looks like the objects in the collection are mutable, and you change them while iterating. This part is fine. But why do you need to add another copy, when you change it? (this part is not fine and causing the exception)

Henry


public void wijzigAantal(int bierNr, int aantal)
{
Iterator it = mandje.iterator();
bestellijn = new BestellijnDTO();

while (it.hasNext())
{
bestellijn = (BestellijnDTO) it.next();

if (bestellijn.getBierNr() == bierNr)
{
bestellijn.setAantal(aantal);
}

}

}

changed it to this, and it works like a charm :-)

thanks for the help guys,

the reason why i added the DTO again, was because I thought, that I pulled it out of the collection and had to put it back in, where I actually am using a reference to the data, and it actually stays where it is. I just had to retreive and update it (correct me if I'm wrong)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!