• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

collection class

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic