• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterator with an ArrayList

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So here's the question, I read the docs, and I thought

Returns true if the iteration has more elements meant it would go until the end of the interation and then return false, but clearly I'm mistaken about it because when I compile and run this code:

import java.util.*;

public class IterTest {

public static void main(String[] args) {

ArrayList<String> myList = new ArrayList<String>();

myList.add("Hello");
myList.add("hi");
myList.add("hola");

Iterator it = myList.iterator();

while(it.hasNext()) {

System.out.println("It worked");

}

} // end of main method

} // end of class

It prints out It worked over and over again until I kill the operation.

So, obviously it's the way I'm reading it that's throwing me off. Can some explain this a little clearer to me.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while(it.hasNext()) {
//System.out.println("It worked");
System.out.println(it.next());
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nicholas,

Your reading is correct, except that hasNext() only queries the state of the Iterator -- it doesn't change it. You have to use the "next()" method to actually retrieve the next element and advance the iterator -- i.e.,

 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, so that makes more sense. If I don't go to the next element, it will stay there forever, but by calling next() it actually brings me to the next element. It was looping before, because it was never making it to the next element.

Thanks again.
[ July 07, 2005: Message edited by: Nicholas Carrier ]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, speaking about ArrayList and Iterators, is there an advantage of iterating the ArrayList with a "for loop" or a "while loop"?

I've usually used the while loop, but other people suggest to use a "for loop", because this way the Iterator only is declared among the "for scope" and this leads to less mistakes. Is that true?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jorge Blaz:
Is that true?



Yes, the for loop is better for that reason, and also because most of the loop control code is all together on one line (although not the "next()" call.)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And of course with J2SE 5.0, you can use the enhanced for:

(Here myList is a List<String> as in the first post above.) Now we get the entire loop control in one line, with no need to write casts (they're handled implicitly for us). The only limitation is that if you needed to use other methods of the Iterator, like remove(), you can't do that with the henhanced for loop. The iterator is hidden from us - hasNext() and next() are called for us implicitly, but other methods are unavailable unless we revert to the traditional (pre-5.0) use of Iterator. Usually this is not a problem, but it's somethign to be aware of.
[ July 12, 2005: Message edited by: Jim Yingst ]
 
Jorge Blaz
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
And of course with J2SE 5.0, you can use the enhanced for:




Wow! how simple...
I've to learn J2SE 5.0

Regarding J2SE 5.0, I've a question maybe you have an answer to...
If I want to use J2SE 5.0 new features what is the least version of J2EE I have to use?. I mean, i guess it is not possible to use J2SE 5.0 features with J2EE 1.3, maybe the minimal would be J2EE 1.4?. Am I wrong?

Thanks

[ July 12, 2005: Message edited by: Jorge Blaz ]

[ July 12, 2005: Message edited by: Jorge Blaz ]
[ July 12, 2005: Message edited by: Jorge Blaz ]
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I've usually used the while loop, but other people suggest to use a "for loop", because this way the Iterator only is declared among the "for scope" and this leads to less mistakes. Is that true?


Yes, I always use a for loop if I can. Many people do not realise that the scope of the Iterator variable can be contained in the scope of the loop:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic