Use the Iterator returned from ArrayList to loop through an array list in Java
Description
The following code shows how to use the Iterator returned from ArrayList to loop through an array list.
Example
// w w w .jav a2 s . c o m
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] a) {
ArrayList<String> nums = new ArrayList<String>();
nums.add("O");
nums.add("Two");
nums.add("Three");
nums.add("java2s.com");
String s;
Iterator e = nums.iterator();
while (e.hasNext()) {
s = (String) e.next();
System.out.println(s);
}
}
}
The code above generates the following result.