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 -
-
public void removeEntry(AddressBookEntry en) throws AddressBookException{
-
LinkedList<Connection> temp;
-
temp = en.getConnections();
-
if(!temp.isEmpty()){
-
for(Connection con : temp){
-
en.deleteConnection(con);
-
}
-
}
-
entries.remove(en);
-
}
-
-
10 1553 BigDaddyLH 1,216
Recognized Expert Top Contributor
First, just for fun, that could have been written more succinctly as: - public void removeEntry(AddressBookEntry en) throws AddressBookException{
-
for(Connection con : en.getConnections()){
-
en.deleteConnection(con);
-
}
-
entries.remove(en);
-
}
(why test for empty when the loop does that for you.)
Second, you forgot to mention which line throws the exception!
the loop itself
line 5 :)
thanks for the tip
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?
-
public void deleteConnection(Connection c) throws AddressBookException{
-
AddressBookEntry en;
-
if( c.o2 == this){
-
en = (AddressBookEntry) c.o1;
-
}
-
else{
-
en = (AddressBookEntry) c.o2;
-
}
-
connections.remove(c);
-
en.connections.remove(c);
-
}
-
-
"connection s" is an attribute (LinkedList) of the AddressBookEntr y class
BigDaddyLH 1,216
Recognized Expert Top Contributor -
public void deleteConnection(Connection c) throws AddressBookException{
-
AddressBookEntry en;
-
if( c.o2 == this){
-
en = (AddressBookEntry) c.o1;
-
}
-
else{
-
en = (AddressBookEntry) c.o2;
-
}
-
connections.remove(c);
-
en.connections.remove(c);
-
}
-
-
"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.
so thats not allowed?
do you know a way to avoid it?
maybe use a usual for or a while loop?
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: - for(Connection con : en.getConnections()){
-
en.deleteConnection(con);
-
}
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: - for(Iterator<Connection> it = getConnections(); it.hasMore(); ) {
-
Connection con = it.next();
-
....
-
if (condition)
-
it.remove();
-
}
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. -
public void deleteAllConnections() throws AddressBookException{
-
AddressBookEntry en;
-
for(Iterator<Connection> it = this.getConnections().listIterator(); it.hasNext();) {
-
Connection con = it.next();
-
// define second connected object
-
if( con.o2 == this) en = (AddressBookEntry) con.o1;
-
else en = (AddressBookEntry) con.o2;
-
// remove connection from both elements
-
en.connections.remove(con);
-
it.remove();
-
}
-
}
-
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: - public void deleteAllConnections() throws AddressBookException{
-
for(Connection con : getConnections()) {
-
AddressBookEntry en = (AddressBookEntry) (con.o2 == this? con.o1 : con.o2);
-
en.connections.remove(con);
-
}
-
getConnections().clear();
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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)
|
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...
|
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...
|
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()?
| |
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'
....
|
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
|
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:
|
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...
|
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
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |