Solved

for iterator

Posted on 2011-03-18
5
248 Views
Last Modified: 2012-05-11
I've just come across this code:

Iterator myIterator = myArrayList.iterator();
for (; myIterator.hasNext(); ){
    String string = (String) myIterator.next();
    // do stuff
}

Open in new window


Is there a reason for using a for loop as opposed to a while loop?
0
Comment
Question by:allelopath
5 Comments
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 50 total points
ID: 35168784
can't think of any... IMO, "while" suits best here!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35168789


type "for loop vs while loop Java" in Google  - tons of links and tons of opinions
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 50 total points
ID: 35168802
I'd also think that when you don't have known number of iterations but rather logical condition
while is more suitable, when I have number I rather use for - easier to read and understand
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 35169323
Its really personal preference

these days you can simplify it to

for (String string : myArrayList) {
    // do stuff
0
 
LVL 1

Author Comment

by:allelopath
ID: 35169901
>>these days you can simplify
ah, that's why it was done. It is old code, probably written before enhanced for loops
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question