• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collection class

 
kuchi DASS
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i tried to run this simple program
But ran with an error. please suggest

import java.util.*;
public class Collectionusage
{
int array[] = { 1,2,3,4 };
Collection col = new ArrayList();
Iterator iter;
public Collectionusage()
{

}
public void initialise()
{
iter = col.iterator();
for(int i=0;i<array.length;i++)
col.add(new Integer(array[i]));

}
public void read()
{

while (iter.hasNext())
{
Integer element = (Integer)iter.next();
//System.out.println("value is"+element.intValue());
}
}
public static void main(String arg[])
{
Collectionusage test = new Collectionusage();
test.initialise();
test.read();
}
}
it complies fine but gives a run time error as follows
java.util.ConcurrentModificationException at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:444) at java.util.AbstractList$Itr.next(AbstractList.java:417) at Collectionusage.read(Collectionusage.java:24) at Collectionusage.main(Collectionusage.java:32) Exception in thread "main" Process Exit...
 
Yosi Hendarsjah
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kuchi!
The member variable iter is initialised before the Collection col is populated. So iter represents an empty collection. Then col is populated. Now there is an inconsistence between iter and col. So when the method read() try to iterate iter,
ConcurrentModificationException is thrown.
 
Sean Walker
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'm so preoccupied with problem solving (due to my efforts on SCJP) everything I see presents itself as an other problem to solve!
So, what's with your name? Kuchi -your first 'name'- happens to mean mouth in Japanese. Of course this might just be coincidence. Anyway, what on earth does DASS mean?
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, Sean this forum is for discussing topics related to SCJP and not posting personal messages.
Thank you.
 
Sean Walker
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abdullasm,
You are quite correct, my mistake. I'll take all future 'personal' comments elsewhere. However, forgive me if I make one last 'personal' observation: I really think you need to get a lot more fibre in your diet man.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic