This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

remove the element in list while iterating through it.

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try using a for-loop with the index. Also using Enhanced For loop for this would lead to ConcurrentModificationException.
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can change it to:
>
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
>
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you keep looking for it after you've found it? Is it going to be there more than once?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you to use Iterator.

For an example:
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


You are welcome
 
Bharath Raja
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks mohmed,, I'm comfortable with Seetharaman's logic..
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can do it like this:

code:


result:




 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Saloon Keeper
Posts: 10785
231
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works

 
brevity is the soul of wit - shakepeare. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!