Why are there two different kinds of for loops in java?

The first is the original for loop. You initialize a variable, set a terminating condition, and provide a state incrementing/decrementing counter (There are exceptions, but this is the classic).

Up vote 3 down vote favorite 3 share g+ share fb share tw.

I have a for loop, and I am not sure how it works. I am familiar with: for(int I = 0; I Java for-loop link|improve this question edited May 1 '11 at 20:31muntoo3,20831121 asked May 1 '11 at 20:07Ephraim34629 96% accept rate.

The first is the original for loop. You initialize a variable, set a terminating condition, and provide a state incrementing/decrementing counter (There are exceptions, but this is the classic) For that, for (int i=0;i list =....load up with stuff for (String string : list) { System.out. Println(string); } instead of for (int i=0; I it = list.iterator(); while (it.hasNext()) { String string=it.next(); System.out.

Println(string); } In the end it's just syntactic sugar, but rather convenient.

3 Note one limitation of using the shorthand for like this instead of raw Iterator is you cannot use the remove() method of the iterator. Also you cannot get the index count when using iterator style for - which for correlated collections is a deal breaker. – MJB May 1 '11 at 20:16.

There is an excellent summary of this feature in the article The For-Each Loop. It shows by example how using the for-each style can produce clearer code that is easier to read and write.

Wow ... a dead tie! – Brian Roach May 1 '11 at 20:10 @Brian Roach: You can get millisecond precision timestamps from the data dump. :) – Greg Hewgill May 1 '11 at 20:11 Burn.

You're such a ray of sunshine, Greg. :) – Brian Roach May 1 '11 at 20:12.

Using the first for-loop you manually enumerate through the array by increasing an index to the length of the array, then getting the value at the current index manually. The latter syntax is added in Java 5 and enumerates an array by using an Iterator instance under the hoods. You then have only access to the object (not the index) and you won't be able to adjust the array while enumerating.

It's convenient when you just want to perform some actions on all objects in an array.

The for-each loop was introduced in Java 1.5 and is used with collections (and to be pedantic, arrays, and anything implementing the Iterable interface ... which the article notes): download.oracle.com/javase/1,5.0/docs/gu....

It can be used with any Iterable object – Jeff Storey May 1 '11 at 20:13 Why yes, that's why I edited that comment 4 minutes ago to say I was thinking of something else. – Brian Roach May 1 '11 at 20:21 Sorry. I didn't see the "edited" note.

– JB Nizet May 1 '11 at 20:38.

The new for-each loop is just a short hand for the older loop. The new loop is easier to use for arrays and some iterators, but not as flexible, so the old loop has to be using in some situations, like counting from 0 to 9.

What would this get a downvote.. – Bozho May 2 '11 at 17:07 @Bozho, Cheers. If they can tell me what I could have done better (and that's always possible), bring on the down votes. ;) – Peter Lawrey May 2 '11 at 19:29.

The second for loop is any easy way to iterate over the contents of an array, without having to manually specify the number of items in the array(manual enumeration). It is much more convenient than the first when dealing with arrays.

The For-each loop, as it is called, is a type of for loop that is used with collections to guarantee that all items in a collection are iterated over. For example for ( Object o : objects ) { System.out. Println(o.toString()); } Will call the toString() method on each object in the collection "objects".

One nice thing about this is that you cannot get an out of bounds exception.

Something none of the other answers touch on is that your first loop is indexing though the list. Whereas the for-each loop is using an Iterator. Some lists like LinkedList will iterate faster with an Iterator versus get(i).

This is because because link list's iterator keeps track of the current pointer. Whereas each get in your for i=0 to 9 has to recompute the offset into the linked list. In general, its better to use for-each or an Iterator because it will be using Collections iterator, which in theory is optimized for the collection type.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions