• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

removal of elements in linkedlist of java

 
dilip satyaraghava
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i try to remove an element from linked list, an exception is arised.. java.util.ConcurrentModificationException...can any one suggest an example for removing element.......
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are you trying to remove the elements from the List while iterating with the for-each loop? Could you please show the code you have tried?
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't remove elements from a list while you are looping through that list.

You could (although I'm not sure if it is the best solution) add all the items that you want to remove to a new list, then call removeAll() on your original list and pass in the new list (outside of a loop).
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use an explicit Iterator and use that to remove elements.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We would like to see the code!
But I think your code is doing what John has mentioned and you can correct it using Rob's suggestion.
This query has come up lot may times before and I am sure its kind of popular exception
 
dilip satyaraghava
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code:

 
dilip satyaraghava
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed few lines in the above code.. This is my code...


import java.util.*;
class Linkedlist
{
public static void main(String args[])
{
LinkedList<Integer> ll=new LinkedList<Integer>();
System.out.println("Size="+ll.size());
System.out.println("Content="+ll);
ll.add(10);
ll.add(20);
ll.add(30);
System.out.println("Size="+ll.size());
System.out.println("Content="+ll);
ll.addFirst(5);
ll.addLast(40);
System.out.println("Size="+ll.size());
System.out.println("Content="+ll);
System.out.println("Iterator -FD");
Iterator<Integer> itr=ll.iterator(); //Retriving the data
while(itr.hasNext())
{
Integer obj1=itr.next();
System.out.println(obj1);
}

System.out.println("ListIterator -FD");
ListIterator<Integer> litr=ll.listIterator(); //Retriving the data
while(litr.hasNext())
{
Integer obj2=litr.next();
System.out.println(obj2);
}
System.out.println("List Iterator-BD");
while(litr.hasPrevious())
{
Integer obj3=litr.previous();
System.out.println(obj3);
}
Integer obj=ll.get(1);
System.out.println("Element ="+obj);
ll.remove(2);
while(litr.hasNext())
{
Integer obj7=litr.next();
System.out.println(obj7);
}}
}
Output:
Size=0
Content=[]
Size=3
Content=[10, 20, 30]
Size=5
Content=[5, 10, 20, 30, 40]
Iterator -FD
5
10
20
30
40
ListIterator -FD
5
10
20
30
40
List Iterator-BD
40
30
20
10
5
Element =10
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
at java.util.LinkedList$ListItr.next(Unknown Source)
at Linkedlist.main(Linkedlist.java:46)
 
Joseph Tulowiecki
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea right here:



you can't change values in an object while your iterating through it.
ll.remove(2);
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either you can reassign the iterator after removing an element

or remove the element using the iterator

 
dilip satyaraghava
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the remove function is after the while iteration, so why do i again need to create an object??

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the first list iterator you created has knowledge about a list that has 5 elements.

You then go and remove one of the elements from the list.

When you try again to use the same list iterator that still don't know about the removal, you get the exception.

So either
1. Re-assign it to a newly created list iterator which knows about the current 4 element list
2. Or remove the element using the list iterator itself. This way the iterator knows one element is removed from the iterating list.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic