Results 1 to 3 of 3
  1. #1
    chawlakunal is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Unhappy Iterator over Iterators

    Hi,

    I am trying to write an ItIterator class which would iterate over the elements of multiple iterator.
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
     
     
    public class ItIterator<T> implements Iterator<T> {
     
    	private Iterator<T> currIterator=null;
    	private ArrayList<Iterator<T>> arr;
    	private Iterator<Iterator<T>> arrIterator;
    	
    	public ItIterator()
    	{
    		arr=new ArrayList<Iterator<T>>();
    		arrIterator=arr.iterator();
    	}
    	
    	public void add(Iterator<T> e)
    	{
    		arr.add(e);
    	}
    	
    	
    	@Override
    	public boolean hasNext() 
    	{
    		if(currIterator!=null && currIterator.hasNext())
    			return true;
    		
    		while(arrIterator.hasNext())
    		{
    			currIterator=arrIterator.next();
    			if(currIterator.hasNext())
    				return true;
    		}
    		return false;
    	}
     
    	@Override
    	public T next() 
    	{
    		if(currIterator!=null && currIterator.hasNext())
    			return currIterator.next();
    		
    		while(arrIterator.hasNext())
    		{
    			currIterator=arrIterator.next();
    			if(currIterator.hasNext())
    				return currIterator.next();
    		}
    		return null;
    	}
     
    	@Override
    	public void remove() 
    	{
    		if(currIterator!=null && currIterator.hasNext())
    			currIterator.remove();
    		
    		while(arrIterator.hasNext())
    		{
    			currIterator=arrIterator.next();
    			if(currIterator.hasNext())
    			{
    				currIterator.remove();
    				return;
    			}
    		}
    		
    	}
     
    }
    
    [CODE]
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Set;


    public class Test
    {



    public static void main(String args[])
    {
    ItIterator<Integer> i=new ItIterator<Integer>();

    ArrayList<Integer> a=new ArrayList<Integer>();
    a.add(1);
    a.add(2);
    a.add(3);
    ArrayList<Integer> b=new ArrayList<Integer>();
    b.add(4);
    b.add(5);
    b.add(6);

    i.add(a.iterator());
    i.add(b.iterator());

    while(i.hasNext())
    System.out.println(i.next().toString());

    }


    }

    [/CODE]


    Te above code complies without any error but gives a runtime exception as follows:
    Java Code:
    Exception in thread "main" java.util.ConcurrentModificationException
    	at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
    	at java.util.AbstractList$Itr.next(Unknown Source)
    	at ItIterator.hasNext(ItIterator.java:31)
    	at Test.main(Test.java:28)

    If someone could help me out solving this exception that would be great.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Have you read the doc for the ConcurrentModificationException ?
    For example:
    it is not generally permissible for one thread to modify a Collection while another thread is iterating over it
    Try doing the following AFTER all the add()s are done
    arrIterator=arr.iterator();
    Last edited by Norm; 05-22-2010 at 08:57 PM.

  3. #3
    chawlakunal is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  2. Iterator help
    By alpdog14 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 08:42 PM
  3. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 01:32 PM
  4. iterator issues
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 01:43 PM
  5. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 02:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •