aspose file tools*
The moose likes Java in General and the fly likes removal of elements in linkedlist of java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "removal of elements in linkedlist of java" Watch "removal of elements in linkedlist of java" New topic
Author

removal of elements in linkedlist of java

dilip satyaraghava
Greenhorn

Joined: Nov 22, 2011
Posts: 8
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Apr 23, 2009
Posts: 160

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).


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Or use an explicit Iterator and use that to remove elements.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3096
    
  33

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


Mohamed Sanaulla | My Blog
dilip satyaraghava
Greenhorn

Joined: Nov 22, 2011
Posts: 8
This is my code:

dilip satyaraghava
Greenhorn

Joined: Nov 22, 2011
Posts: 8
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

Joined: Jun 10, 2011
Posts: 25
Yea right here:



you can't change values in an object while your iterating through it.
ll.remove(2);
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Either you can reassign the iterator after removing an element

or remove the element using the iterator

dilip satyaraghava
Greenhorn

Joined: Nov 22, 2011
Posts: 8
the remove function is after the while iteration, so why do i again need to create an object??

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: removal of elements in linkedlist of java