aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes collection class 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "collection class" Watch "collection class" New topic
Author

collection class

kuchi DASS
Greenhorn

Joined: Sep 02, 2003
Posts: 6
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

Joined: Oct 02, 2003
Posts: 164
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

Joined: Feb 04, 2004
Posts: 64
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?


Sean Walker<br />PMP, SCEA, SCWCD, SCJP
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
I am sorry, Sean this forum is for discussing topics related to SCJP and not posting personal messages.
Thank you.
Sean Walker
Ranch Hand

Joined: Feb 04, 2004
Posts: 64
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: collection class