Solved

for iterator

Posted on 2011-03-18
5
244 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get weblogic logged in user in java 2 57
Error with Java/Cache JDBC Classpath 2 30
Java Options in Zimbra Server 2 46
replace a word with other 1 40
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now