Java ArrayList Iterator Function

iterator function

What is an ArrayList?

Arraylist is a part of collection framework in java. we can perform several inbuilt functions of different classes of collection. The iterator function of java.util.Iterator Package can be used to iterate over the ArrayList in Java.
It implements the List interface of the collections framework.

Here, in the page we will discuss about the Iterator Method in java. To  know more about ArrayList in java, you can click on the button given below.

ArrayList Iterator Function:

Java Collection contains several inbuilt functions that are used to perform several operations on ArrayList. The Iterator function is an inbuilt method of class Iterator.
The Java ArrayList Iterator function helps to iterate over the ArrayList. It returns an iterator over the elements in this list.

Syntax:

arraylist_name.iterator()

Definition of Parameters:

Return Type :

iterator method in java

clear() Function in ArrayList

clone() Function in ArrayList

add() Function in ArrayList

addall() Function in ArrayList

Example for Iterating over the elements of ArrayList of Integers:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        ArrayList<Integer> Prep = new ArrayList<>();
        Prep.add(10);
        Prep.add(20);
        Prep.add(30);
        Prep.add(40);
        
        System.out.println("ArrayList Prep : " + Prep);
        Iterator<Integer> Insta = Prep.iterator();
        System.out.print("The Iterator Values in ArrayList are : ");
        while(Insta.hasNext()){
            System.out.print(Insta.next() + " ");
        }
    }
}
Output:

ArrayList Prep : [10, 20, 30, 40]
The Iterator Values in ArrayList are : 10 20 30 40 

In the above Example, We had taken an ArrayList “Prep” and an Iterator named “Insta” of integer type, we are iterating over the elements of the ArrayList “Prep” with the help of the Iterator and functions like:

  • hasNext() : This function helps to tell the compiler whether the next element of the ArrayList is available or not.
  • next() : This function is used to move to the next element of the ArrayList.

Example for Iterating over the elements of ArrayList of Strings:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        ArrayList<String> Prep = new ArrayList<>();
        Prep.add("We");
        Prep.add("Are");
        Prep.add("Prepsters");
        
        System.out.println("ArrayList Prep : " + Prep);
        Iterator<String> Insta = Prep.iterator();
        System.out.print("The Iterator Values in ArrayList are : ");
        while(Insta.hasNext()){
            System.out.print(Insta.next() + " ");
        }
    }
}
Output:

ArrayList Prep : [We, Are, Prepsters]
The Iterator Values in ArrayList are : We Are Prepsters 

In the above Example. we have created an ArrayLists “Prep” and an Iterator “Insta” of String Type, we are iterating over the ArrayList “Prep” with the help of iterator functions in the loop and iterator “Insta”.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription