Solved

for iterator

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 76
Cipher Configuration on Apache HTTPD 4 64
Error in @AspectJ Based AOP with Spring 2 15
servlet example 17 33
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

825 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