aspose file tools*
The moose likes Java in General and the fly likes remove the element in list while iterating through it. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "remove the element in list while iterating through it." Watch "remove the element in list while iterating through it." New topic
Author

remove the element in list while iterating through it.

Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
how can we remove the element within array list while iterating through it..
My primary requirement is, comparing the current builded model object with model within list, if its match then remove the model from list..

My code as,



let us take the list contains 2 elements initially. Now the problem is, element is removed from list at first iteration, but at the second time myIt.hasNext() condition fails, because of the list contains only one item now.. Can anyone suggest me regarding this...

Please remember, I need to iterate through the list from head to toe(all elements), and remove the repeated elements...


Life is either daring something or nothing - Helen Keller
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

You can try using a for-loop with the index. Also using Enhanced For loop for this would lead to ConcurrentModificationException.


Mohamed Sanaulla | My Blog
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Mohamed Sanaulla wrote:You can try using a for-loop with the index. Also using Enhanced For loop for this would lead to ConcurrentModificationException.


I've tried with for loop also,, codes look like,,


but this also doesn't work... this leads to ArrayIndexOutOfBoundException, because of after removing element in index 0, element in index 1 comes to 0'th position...
>
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

You can change it to:
>
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Mohamed Sanaulla wrote:You can change it to:

not working... because of we've increased the i value, at second time i is 1 and list size is also 1, condition fails.. now I'm checking
>
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Why do you keep looking for it after you've found it? Is it going to be there more than once?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I would suggest you to use Iterator.

For an example:
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Tom Reilly wrote:Why do you keep looking for it after you've found it? Is it going to be there more than once?

yes.. let me clear my scenario first,, the list has get values from xml file while its loading,, after that i'm gonna to build my MyModel class which will have half of its member has values half of them or not.. now I'm using the removeFromList() method for validation.. Because of, the model in xml have all its member values, this method remove nothing now..more than that I've add it to myList. Now my list contains 2 elements,, model1 from xml file, model 2 from operation occurs now,, after that i'll add remaining member values into current builded model, that is updated in current model which is in the list (index position 1)... So, now myList has 2 same elements( assumes each model value has same member values)... Now I'm once again call my removeFromList() method.. From this method I'm expecting remove both elements from list.. list's clear() method will not help me here because of future consideration...
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

You can try this:
Code deleted as it was wrong and would lead to java.lang.IndexOutOfBoundsException exception. Correct code posted below.

Update:
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
Seetharaman Venkatasamy wrote:I would suggest you to use Iterator.

For an example:

this works fine, and achieve my requirement exactly.. thanks folks...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Bharath Raja wrote:this works fine, and achieve my requirement exactly.. thanks folks...

You are welcome
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
thanks mohmed,, I'm comfortable with Seetharaman's logic..
antty ge
Greenhorn

Joined: Dec 24, 2010
Posts: 7
you can do it like this:

code:


result:




Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

When you remove elements from a list that you are iterating over, then the iterator will get confused and it will throw a ConcurrentModificationException when you get the next element. Instead of calling remove() on the list you should call remove() on the iterator. That will remove the last element that the iterator saw from the list and make sure that the iterator doesn't get confused.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3366
    
    9
Yup either that, or what I sometimes do is iterate through the list *backwards*. That way, there is no confusion when you delete an element.
Manohar Reddy Gopireddy
Greenhorn

Joined: Feb 16, 2011
Posts: 10
This works

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: remove the element in list while iterating through it.
 
Similar Threads
LinkedList Question
How to remove an element from LinkedList while traversing
How to remove duplicate element from List
Can we add an element in hashtable while iterating
removal of elements in linkedlist of java