473,396 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

got to delete an element from a collection

Ive got a collection with values
Object[0]= hashmap(k,v)
Object[1]= hashmap(k,v)
Object[2]= hashmap(k,v)
Object[3]= hashmap(k,v)
Object[4]= hashmap(k,v)

i terated this collection and deleted object[1]. then when i iterate again i get the following exception.
java.util.ConcurrentModificationException

i want to delete elements till Object[3].
how do i do this
please help
Mar 30 '07 #1
11 2915
r035198x
13,262 8TB
Ive got a collection with values
Object[0]= hashmap(k,v)
Object[1]= hashmap(k,v)
Object[2]= hashmap(k,v)
Object[3]= hashmap(k,v)
Object[4]= hashmap(k,v)

i terated this collection and deleted object[1]. then when i iterate again i get the following exception.
java.util.ConcurrentModificationException

i want to delete elements till Object[3].
how do i do this
please help
How did you delete? Post your code for the deleting part
Mar 30 '07 #2
How did you delete? Post your code for the deleting part

for (Iterator iter = voucherTransactionCols.iterator(); iter.hasNext();) {
Object object = (Object) iter.next();
HashMap campusModel = (HashMap) object;

voucherTransactionCols.remove(campusModel);
}

Now i can delete the elements but when i delete Object[3] , the element Object[4] moves to the third position . so i cant delete the last elenment in the collection.
Mar 30 '07 #3
dmjpro
2,476 2GB
see this code carefully .....

Collection c = some code here;

Label:
for(int i=0;i<c.size();i++)
if(condition)
{
c.remove(c.get(i));
continue Label;
}

try this code ..........
all the best
Mar 30 '07 #4
see this code carefully .....

Collection c = some code here;

Label:
for(int i=0;i<c.size();i++)
if(condition)
{
c.remove(c.get(i));
continue Label;
}

try this code ..........
all the best

i cannot get the method c.get(i);
Mar 30 '07 #5
dmjpro
2,476 2GB
sorry ..... i just use general convention .....
now i use according to ur code ......

label:
for (Iterator iter = voucherTransactionCols.iterator(); iter.hasNext() {
Object object = (Object) iter.next();
HashMap campusModel = (HashMap) object;

voucherTransactionCols.remove(campusModel);
continue label;
}

try this code ...... all the best ....
i think u understood my logic
Mar 30 '07 #6
sorry ..... i just use general convention .....
now i use according to ur code ......

label:
for (Iterator iter = voucherTransactionCols.iterator(); iter.hasNext() {
Object object = (Object) iter.next();
HashMap campusModel = (HashMap) object;

voucherTransactionCols.remove(campusModel);
continue label;
}

try this code ...... all the best ....
i think u understood my logic
now im getting java.util.ConcurrentModificationException after deleting the first element and going for the next iteration.
wat to do
Mar 30 '07 #7
dmjpro
2,476 2GB
wait plzzzz ... i m trying to solve it ......
Mar 30 '07 #8
dmjpro
2,476 2GB
it is because of removing and iterating concurrently...

so if u post ur code ...... then i think i will give u some hope....


thanxxxxx
Mar 30 '07 #9
it is because of removing and iterating concurrently...

so if u post ur code ...... then i think i will give u some hope....


thanxxxxx

hey i got it
this is my code
label:
for (Iterator iter = voucherTransactionCols.iterator(); iter.hasNext();) {
Object object = (Object) iter.next();
HashMap campusModel = (HashMap) object;
iter.remove();
continue label;
}

}

}

Use the iterator to remove....... instead of coll.remove()

Thanks a lot
Mar 30 '07 #10
r035198x
13,262 8TB
Here is a hint from the docs


"The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future"
Mar 30 '07 #11
r035198x
13,262 8TB
hey i got it
this is my code
label:
for (Iterator iter = voucherTransactionCols.iterator(); iter.hasNext();) {
Object object = (Object) iter.next();
HashMap campusModel = (HashMap) object;
iter.remove();
continue label;
}

}

}

Use the iterator to remove....... instead of coll.remove()

Thanks a lot
And it doesn't look like you'd seen my hint too. Very good work.
Mar 30 '07 #12

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

Similar topics

2
by: Michal Januszczyk | last post by:
Hello. Sometimes I encounter the following problem: foreach( element of some collection) { delete this element; } of course, the runtime would not allow to modify
2
by: Tomomichi Amano | last post by:
Hello How can I delete (clear) lines that were made useing Graphics.DrawLine() ? Thanks in advance! Have a nice day!
7
by: Marc Bishop | last post by:
Hi can anyone help? I'm making a shopping cart and am stuck on removing an item from my array? The array is made : cArray(ITEM_NAME,cItem) = ProductName
7
by: Mike | last post by:
Hi, I created such procedure for deleting element from my dynamic array because I could not find such possibility from .Net . Private aintABC() As Int32 '' Initialization somewhere in...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
14
by: hgraham | last post by:
Hi, I'm trying to understand how to work the dom, and all I'm trying to do is insert a link right before another link in the html based on it's href value. This isn't a real world example - I'm...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
1
by: shiniskumar | last post by:
how to delete the last element of a collection without iterating it?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.