Skip to content

Instantly share code, notes, and snippets.

@webcane
Last active December 14, 2015 20:19
Show Gist options
  • Select an option

  • Save webcane/5143471 to your computer and use it in GitHub Desktop.

Select an option

Save webcane/5143471 to your computer and use it in GitHub Desktop.
different ways to looping over ArrayList
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