aspose file tools*
The moose likes Beginning Java and the fly likes Iterator with an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterator with an ArrayList" Watch "Iterator with an ArrayList" New topic
Author

Iterator with an ArrayList

Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
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.


Teaching yourself anything is always the cheapest way, but it definitely takes a lot of time and effort.<br /> <br />Thank you javaranch <a href="http://"http://faq.javaranch.com/view?HowToAskQuestionsOnJavaRanch"" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
while(it.hasNext()) {
//System.out.println("It worked");
System.out.println(it.next());
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.,



[Jess in Action][AskingGoodQuestions]
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
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 ]
Jorge Blaz
Ranch Hand

Joined: Jan 31, 2004
Posts: 53
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Jorge Blaz
Ranch Hand

Joined: Jan 31, 2004
Posts: 53
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 ]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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:


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterator with an ArrayList