Display arraylist values by using iterator in java

Example:

package com.w3schools;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class Test {
  public static void main(String args[]){
	ArrayList<String> list = new ArrayList<String>();
	list.add("Jai");
	list.add("Mahesh");
	list.add("Vivek");
	list.add("Naren");
	list.add("Hemant");
	list.add("Vishal");
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
  }
}

Output:

Jai
Mahesh
Vivek
Naren
Hemant
Vishal