Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

List iterators question

Thread view: 
Oxnard - 28 Jan 2006 03:21 GMT
Here's a real simple Iterator example done in 1.4

public static void main(String[] args){
List list = new ArrayList();
String[] sa = new String[2];
// put some stuff in the list
for(int i = 0;i < 10; i++){
sa[0] = " this is sa[0] list position " + i + " ";
sa[1] = " this is sa[1] list position " + i + " ";
list.add(sa);
}
System.out.println("list has " + list.size() + " elements");
// iterate through the list
for(Iterator it=list.iterator(); it.hasNext();){
String[] saw =(String[]) it.next();
System.out.println(saw[0] + saw[1]);
}
}

When I run this I get:

list has 10 elements
this is sa[0] list position 9 this is sa[1] list position 9
this is sa[0] list position 9 this is sa[1] list position 9
this is sa[0] list position 9 this is sa[1] list position 9
this is sa[0] list position 9 this is sa[1] list position 9
this is sa[0] list position 9 this is sa[1] list position 9
this is sa[0] list position 9 this is sa[1] list position 9
this is sa[0] list position 9 this is sa[1] list position 9
this is sa[0] list position 9 this is sa[1] list position 9
this is sa[0] list position 9 this is sa[1] list position 9
this is sa[0] list position 9 this is sa[1] list position 9

What I am not understanding is why I am not seeing:
this is sa[0] list position 0 this is sa[1] list position 0
this is sa[0] list position 1 this is sa[1] list position 1
this is sa[0] list position 2 this is sa[1] list position 2
this is sa[0] list position 3 this is sa[1] list position 3
this is sa[0] list position 4 this is sa[1] list position 4
this is sa[0] list position 5 this is sa[1] list position 5
this is sa[0] list position 6 this is sa[1] list position 6
this is sa[0] list position 7 this is sa[1] list position 7
this is sa[0] list position 8 this is sa[1] list position 8
this is sa[0] list position 9 this is sa[1] list position 9

In the examples I have seen it infers that the Collection.add(Obj) appends
to the list. And that an Interator starts at position 0 and increases. What
am I missing here?

thanks
opalpa@gmail.com opalinski from opalpaweb - 28 Jan 2006 04:44 GMT
The list contains ten refernces to the same array.

If you want to see the former put " String[] sa = new String[2]; " into
population loop.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.