ArrayList iterator() method in java

Let’s learn ArrayList iterator() method in java.

ArrayList iterator() method in java

iterator() method of ArrayList class returns an iterator over the elements in this list in proper sequence.

Syntax:

public Iterator<E> iterator()

Package:

java.util

Now let’s see an example on ArrayList iterator() method.

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListIteratorMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<String> fruits = new ArrayList<String>(7);
      fruits.add("banana");
      fruits.add("mango");
      fruits.add("apple");
      fruits.add("watermelon");
      fruits.add("pineapple");
      fruits.add("orange");
      fruits.add("plum");
      Iterator<String> iterate;
      iterate = fruits.iterator();
      System.out.println("Fruits in ArrayList are: ");
      while(iterate.hasNext())
      {
         System.out.println(iterate.next());
      }
   }
}


Output:

Fruits in ArrayList are:
banana
mango
apple
watermelon
pineapple
orange
plum


Also read – operators in java