Java ArrayList iterator()

The syntax of the iterator() method is:

arraylist.iterator()

iterator() Parameters

The iterator() method does not take any parameters.


iterator() Return Values

Note: The iterator returned by the method is stored in the variable of interface Iterator type.


Example 1: Java ArrayList iterator()

import java.util.ArrayList;
import java.util.Iterator;

class Main {
  public static void main(String[] args){
    ArrayList<String> languages = new ArrayList<>();

    // Add elements in the array list
    languages.add("Java");
    languages.add("Python");
    languages.add("JavaScript");
    languages.add("Swift");

    // Create a variable of Iterator
    // store the iterator returned by iterator()
    Iterator<String> iterate = languages.iterator();
    System.out.print("ArrayList: ");

    // loop through ArrayList till it has all elements
    // Use methods of Iterator to access elements
    while(iterate.hasNext()){
      System.out.print(iterate.next());
      System.out.print(", ");
    }
  }
}

Output

ArrayList: Java, Python, JavaScript, Swift,

In the above example, we have created an arraylist named languages. Notice the line,

Iterator<String> iterate = languages.iterator();

Here, we have created a variable named iterate of the Iterator interface. The variable stores the iterator returned by the iterator() method.

Using iterate, we can access the elements of the arraylist.

  • hasNext: returns true if there is a next element in the arraylist
  • next(): returns the next element in the arraylist

Note: We have used the ArrayList add() method to insert elements to the arraylist.


Example 2: Get Index of Each Element of ArrayList Using iterator()

import java.util.ArrayList;
import java.util.Iterator;

class Main {
  public static void main(String[] args){
    ArrayList<String> languages = new ArrayList<>();

    // Add elements in the array list
    languages.add("Java");
    languages.add("Python");
    languages.add("JavaScript");
    languages.add("Swift");

    // Create a variable of Iterator
    // store the iterator returned by iterator()
    Iterator<String> iterate = languages.iterator();
    System.out.println("Element: Index");

    // loop through ArrayList till it has all elements
    // Use methods of Iterator to access elements
    while(iterate.hasNext()){

      // access element
      String element = iterate.next();
      System.out.print(element + ": ");

      // access index of each element
      System.out.println(languages.indexOf(element));
    }
  }
}

Output

Element: Index
Java: 0
Python: 1
JavaScript: 2
Swift: 3

Note: We have used the Java ArrayList indexOf() method to access the index number of the element.

The ArrayList also provides a listIterator() method to iterate through list only. To learn more, visit Java ListIterator.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges