Posted By

cpjobling on 03/01/07


list iterator foreach

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Iteration through a Generic List (Java 5 version)

 / Published in: Java

This code uses the new Java 5 foreach loop to iterate through every element of a list. It avoids the need to declare an iterator.

  1. List<String> listOfStrings = new LinkedList<String>( );
  3. listOfStrings.add("Why");
  4. listOfStrings.add("Iterate");
  5. listOfStrings.add("When");
  6. listOfStrings.add("You");
  7. listOfStrings.add("Can");
  8. listOfStrings.add("Avoid");
  9. listOfStrings.add("It!");
  11. for (String s: listOfStrings) {
  12. System.out.println(s);
  13. }

Report this snippet  


RSS Icon Subscribe to comments
Posted By: stel on February 14, 2018

can I use this code to iterate a two dimensional list and sort it alphabetically? And will it work on Java 8? Guess once done with working on this web site will try it myself but thought would ask you. Maybe you have some other interesting ideas on how I can do it.

Posted By: sxcaskjc on May 3, 2018

ShowBox Latest
VidMate Latest
Mobdro Latest Thanks for sharing.I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.

Posted By: Mary476 on June 26, 2018

The 4 Methods for Iterating Collections in Java: Mobdro

  1. The Classic For Loop
  2. The Iterator Method
  3. The Enhanced For Loop
  4. The forEach Method with Lambda Expressions
Posted By: peerji on August 13, 2018


Posted By: peter01 on November 4, 2018

Posted By: piyushG on December 21, 2018

Iteration and looping make java programming easy and faster. But we need to take where and why we use these loops and iteration. Misplacement of this thing will make infinite outputs or may generate any other error. You can see it here.

Posted By: piyushG on December 21, 2018

Web developer also uses such type of coding formula in a redirection of web pages. If we want to redirect specific any page to other then it is helpful for it mobdro App.

You need to login to post a comment.