473,749 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble iteration over a list and removing items

14 New Member
Hi guys,

ive written the following method.
its supposed to delete objects in a LinkedList.
These objects implicit Connections to other objects.
if one object is deleted all the connections in the other objects should be deleted as well.

all in all this code should do what I want. the only problem is that i get an ConcurrentModif icationExceptio n running it.

in the API it says
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
unfortunately I cant figure out the problem

would be great if anybody can help.!

Thanks very much

Expand|Select|Wrap|Line Numbers
  1.  
  2.   public void removeEntry(AddressBookEntry en) throws AddressBookException{
  3.         LinkedList<Connection> temp;
  4.         temp = en.getConnections();        
  5.         if(!temp.isEmpty()){
  6.             for(Connection con : temp){
  7.                 en.deleteConnection(con);
  8.             }
  9.         }
  10.         entries.remove(en); 
  11.     }
  12.  
  13.  
Jun 24 '08 #1
10 1553
BigDaddyLH
1,216 Recognized Expert Top Contributor
First, just for fun, that could have been written more succinctly as:

Expand|Select|Wrap|Line Numbers
  1. public void removeEntry(AddressBookEntry en) throws AddressBookException{
  2.     for(Connection con : en.getConnections()){
  3.         en.deleteConnection(con);
  4.     }
  5.     entries.remove(en);
  6. }
(why test for empty when the loop does that for you.)

Second, you forgot to mention which line throws the exception!
Jun 24 '08 #2
daschicken
14 New Member
the loop itself
line 5 :)

thanks for the tip
Jun 24 '08 #3
BigDaddyLH
1,216 Recognized Expert Top Contributor
the loop itself
line 5 :)

thanks for the tip
I don't have all your code in front of me. Does deleteConnectio n remove the Connection from the list, too?
Jun 24 '08 #4
daschicken
14 New Member
Expand|Select|Wrap|Line Numbers
  1.     public void deleteConnection(Connection c) throws AddressBookException{
  2.         AddressBookEntry en;
  3.         if( c.o2 == this){
  4.             en = (AddressBookEntry) c.o1;
  5.         }
  6.         else{
  7.             en = (AddressBookEntry) c.o2;
  8.         } 
  9.         connections.remove(c);
  10.         en.connections.remove(c);       
  11.     }
  12.  
  13.  
"connection s" is an attribute (LinkedList) of the AddressBookEntr y class
Jun 24 '08 #5
BigDaddyLH
1,216 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1.     public void deleteConnection(Connection c) throws AddressBookException{
  2.         AddressBookEntry en;
  3.         if( c.o2 == this){
  4.             en = (AddressBookEntry) c.o1;
  5.         }
  6.         else{
  7.             en = (AddressBookEntry) c.o2;
  8.         } 
  9.         connections.remove(c);
  10.         en.connections.remove(c);       
  11.     }
  12.  
  13.  
"connection s" is an attribute (LinkedList) of the AddressBookEntr y class
That's your error then. You are mutating the list as you iterate over it.
Jun 24 '08 #6
daschicken
14 New Member
so thats not allowed?
do you know a way to avoid it?

maybe use a usual for or a while loop?
Jun 24 '08 #7
BigDaddyLH
1,216 Recognized Expert Top Contributor
The code you posted (from class X?) seems to be doing work that AddressBookEntr y should be doing:

Expand|Select|Wrap|Line Numbers
  1. for(Connection con : en.getConnections()){
  2.     en.deleteConnection(con);
  3. }
Perhaps AddressBookEntr y can avoid exposing a getConnections method and define instead a deleteAllConnec tions()?

Then in that method, use the proper way to remove elements as you iterate: the remove method of Iterator:

Expand|Select|Wrap|Line Numbers
  1. for(Iterator<Connection> it = getConnections(); it.hasMore(); ) {
  2.     Connection con = it.next();
  3.     ....
  4.     if (condition)
  5.         it.remove();
  6. }
Jun 24 '08 #8
daschicken
14 New Member
hi,

your tip gave me the right direction, thanks very much for that!

in case your interested I post my working code. I'm not sure if its coded very nice but as im relatively new to java I'm glad its working at all :D.

Expand|Select|Wrap|Line Numbers
  1.     public void deleteAllConnections() throws AddressBookException{  
  2.         AddressBookEntry en;
  3.         for(Iterator<Connection> it = this.getConnections().listIterator(); it.hasNext();) {
  4.           Connection con = it.next();
  5.       // define second connected object  
  6.         if( con.o2 == this) en = (AddressBookEntry) con.o1;
  7.         else  en = (AddressBookEntry) con.o2;
  8.       // remove connection from both elements  
  9.         en.connections.remove(con);
  10.         it.remove();
  11.       }
  12.     }
  13.  
Jun 25 '08 #9
BigDaddyLH
1,216 Recognized Expert Top Contributor
An alternative to it.remove() would be to call clear() on the list after the loop. Simpler and faster:

Expand|Select|Wrap|Line Numbers
  1. public void deleteAllConnections() throws AddressBookException{  
  2.     for(Connection con : getConnections()) {
  3.         AddressBookEntry en = (AddressBookEntry) (con.o2 == this? con.o1 : con.o2);
  4.         en.connections.remove(con);
  5.     }
  6.     getConnections().clear();
  7. }
Jun 25 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

59
4340
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The HTML version is much more readable than the ReST version. See: http://www.python.org/peps/pep-0322.html
2
1778
by: KraftDiner | last post by:
I have a list, and within it, objects are marked for deletion. However when I iterate through the list to remove objects not all the marked objects are deleted.. here is a code portion: i = 0 for obj in self.objList: if obj.mouseHit: print 'delete + ' + `i` self.objList.pop(i)
3
3115
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand list box. The problem is that if there are many items selected (thousands), then removing the items from the right side list box takes for ever because we are removing them one at a time, which causes the listbox to re-index everything before we...
28
7399
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are "good" in such terms, that this core data structure is changed only by atomic operations, so that the data structure is always consistent regarding the application. Only the change-operations on the dicts and lists itself seem to cause problems...
9
28736
by: Henning Hasemann | last post by:
I'm using a stl-priority queue and want - find out if a certain item is contained in the queue - to be able iterate over all items without having to pop() them, order does not matter. I couldnt find methods for these which suprises me as both should be easily solvable with access to the underlying vector. Did I just miss these methods? Can I somehow access the vector to get . begin() .end() and .find()?
3
989
by: stef | last post by:
hello, can someone tell me why the following iteration doesn't work, and how I should replace empty strings in a list with a default value. .... if items=='': .... items='3' ....
9
4050
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? Bill
23
18080
by: Florian Lindner | last post by:
Hello, can I determine somehow if the iteration on a list of values is the last iteration? Example: for i in : if last_iteration: print i*i else:
1
1708
by: mdpems | last post by:
Hi, I have a combo box that is set up to receive items from another from and post as a record set. The items will post to the list as I click an add item button. I also have a remove item button. I also have a total button at the bottom of my screen that when clicked will total the items in the list. What I want to be able to do is retotal the items in the list after removing an item. Right now if I remove items from my list it will not be...
7
2843
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of RefClass) Implements ICloneable
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.