Iterate through elements ArrayList using Iterator in Java
Description
The following code shows how to iterate through elements ArrayList using Iterator.
Example
// w ww .j av a 2 s . com
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("java2s.com");
Iterator itr = arrayList.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
}
}
}
The code above generates the following result.