• Post Reply Bookmark Topic Watch Topic
  • New Topic

ConcurrentModificationException  RSS feed

 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using a code to remove elements from an arraylist but I receive an error whan the units of that product are->is only 1

2
Indica el código del producto a eliminar
5
Producto eliminado
error: java.util.ConcurrentModificationException





I have seen several solutions in internet, but none of them is working correctly in my program:





Has anybody an idea about a possible solution?

Thanks in advance.
 
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

When iterating through the arraylist, you can't add or remove items directly to / from the list. You must use the iterator. This means that you can't use the enhanced for loop (as that hides the iterator).

Henry
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohoh!

I think I have made something wrong, I have found following example in internet:


I have modified my program:

But the product still stays there when I list my products.....What is wrong with this code?
I don't understand how nombre could be = cod, what is nombre in this ListIterator?

Thank you in advance.

 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get a concurrent modification exception when you have 2 separate references into something and one of them modifies that something but there's no mechanism in place to allow the other referrer to handle the change.

Think of it like running up a set of stairs. You're going bump, bump, bump, bump, up the stairs. But while you're running up, some other person removes one of the steps right before you're about to step on it.

Or maybe you're counting them and someone removes 2 or 3 steps behind you and you can only see forward. Your count is no longer any good, but since your count is based on actually stepping on stairs, you don't realize it until someone tells you. The ConcurrentModificationException is basically that someone telling you.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Perfect explanation I understand now what is ConcurrentModificationException . Thank you very much, it's a good way to know what is wrong each time.

So, there is necessity for change my code, or it doesn't matter if someone see that. I suppose I have to change it because it is not really cute reading that after executing an instruction , but if I have not, please tell me & continue with other thing of the program.

I have tried this time following code:

But in line 5 I receive a red exclamation mark= int cannot be dereferenced.

Thanks.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously codigo is an int and you cannot write i.foo() if i is an int.
You would have to write codigo == xxx or xxx.getCodigo() == yyy.

And don't write }}}}} with more than one brace on the same line. That is a good recipe for getting your {} unbalanced and logic errors difficult to find.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might do better to have a sameCode() method in the Producto class. That will reduce the number of . in the expression.
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In order to reduce the ., thank you & yes, I think it will be better to have sameCode() in product class, but I have 5 classes only for Stock that's because the code is so long because of this structure:
Product (product is defined)
Stock (where I have all my methods)
.
.
.
ok, I have changed the instructions, now I have this code, trying to make a copy of the iterator to my array


I have a red exclamation mark in this line telling me: Iterator <Producto> cannot be converted to ArrayList<Producto>
If it is supposed that I need to use iterator, & I remove the elements of Iterator, maybe I need to see Iterator instead of stock.
Step after step I understand less of iterators. Please could anybody explain to me the sense of iterator & how should I use it?

Thank you in advance.
 
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
Ruth Jordan wrote:

I have a red exclamation mark in this line telling me: Iterator <Producto> cannot be converted to ArrayList<Producto>
If it is supposed that I need to use iterator, & I remove the elements of Iterator, maybe I need to see Iterator instead of stock.
Step after step I understand less of iterators.


An iterator is an external object to the collection. It is not the same as a collection (obvious, since it has a different API). So, you can't assign an iterator to a collection reference variable.

Henry
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm, and then?, if I'm using an iterator because it is not possilbe to delete an item of my arraylist directly, and i delete it from the iterator, how can I obtain the correct information to my arraylist?

Thank you very much.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please define correct information to your arraylist.
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some things to note:


Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.


This is from the official JavaDoc for Iterator.

Looking at the docs for ArrayList:

The returned iterator is fail-fast.


Fail-fast means that you get a ConcurrentModificationException if you alter the list or its elements while iterating. What you do after that is up to you. You may wish to simply discard the current iterator, get a new one and start over, if that doesn't cause any other problems. If at all possible, however, avoid modifying the list while iterating it.

There are some collection and iterator classes that explicitly do allow removing/adding/changing elements while being iterated (part of their "well-defined semantics"). Which ones qualify I cannot recall off-hand, however. One would have to check the documentation. The List interface is neutral on the subject, some iterators provide "safe" modification methods, and some classes (for example, LinkedList) appear to be safe but only if accessed using a selected set of "safe" methods.
 
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
Ruth Jordan wrote:Ummm, and then?, if I'm using an iterator because it is not possilbe to delete an item of my arraylist directly, and i delete it from the iterator, how can I obtain the correct information to my arraylist?


Well, to be honest, with your example, you simply can't use iterators.

You are *not* allowed to delete items from your collection (outside of the iterators that you are iterating with). If you do, the iterator will throw the exception. However, with this example, you have two iterators -- one implicitly created and used with the outer loop, and one that you explicitly created with the inner loop.

You are *not* allowed to delete items outside of the iterators that you are iterating with. So, when you delete the item with the inner loop iterator, you break the outer loop iterator. And of course, vice versa.


I recommend that you don't use iterators -- which of course, means that you can't use the for-each loop either. Or if you can, modify the code to only use one iterator.

Henry
 
Ruth Jordan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ups sorry! I think my basic knowledge of java doesn't let me understand this sentence:
You are *not* allowed to delete items from your collection (outside of the iterators that you are iterating with). If you do, the iterator will throw the exception. However, with this example, you have two iterators -- one implicitly created and used with the outer loop, and one that you explicitly created with the inner loop.

Which iterator is the outer one & which one is the inner one?

Thanks.
 
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
Ruth Jordan wrote:
Which iterator is the outer one & which one is the inner one?


Your latest snippet doesn't show both iterators. You only show the outer one -- which is created implicitly at line 5.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!