[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

for iterator

Posted on 2011-03-18
5
Medium Priority
?
265 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 200 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 200 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 1600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 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