Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

List iterators question
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



Report this thread to moderator Post Follow-up to this message
Old Post
Oxnard
01-28-06 02:57 AM


Re: List iterators question
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/


Report this thread to moderator Post Follow-up to this message
Old Post
opalpa@gmail.com opalinski from opalpaweb
01-28-06 08:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:13 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.