Solved

for iterator

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 93
base64 decode encode 12 100
compre toata in where clue oracle 4 44
Java - Why doesn't this JFrame work 3 18
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

744 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

10 Experts available now in Live!

Get 1:1 Help Now