Last active
December 14, 2015 20:19
-
-
Save webcane/5143471 to your computer and use it in GitHub Desktop.
different ways to looping over ArrayList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| public class Test { | |
| public static void main(String[] args) { | |
| List<String> list = new ArrayList<>(); | |
| list.add("http://stackoverflow.com"); | |
| list.add("http://github.com"); | |
| list.add("http://habrahabr.ru"); | |
| System.out.println("normal for loop"); | |
| for (int i = 0; i < list.size(); i++) { | |
| System.out.println(list.get(i)); | |
| } | |
| System.out.println("The For-Each Loop"); | |
| for (String temp : list) { | |
| System.out.println(temp); | |
| } | |
| System.out.println("while loop"); | |
| int j = 0; | |
| while (list.size() > j) { | |
| System.out.println(list.get(j)); | |
| j++; | |
| } | |
| System.out.println("iterating over a collection"); | |
| Iterator<String> iterator = list.iterator(); | |
| while (iterator.hasNext()) { | |
| System.out.println(iterator.next()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment