Java Program to Iterate over an ArrayList

Java Program to Iterate over an ArrayList

What is ArrayList in Java ?

In Java, an ArrayList is a class that provides a resizable array implementation of the List interface. It allows you to store and manipulate a collection of objects in a dynamic manner, with the ability to add, remove, and access elements by their index.
Unlike a regular array, an ArrayList can grow and shrink in size dynamically as elements are added or removed. This makes it a more flexible data structure that can adapt to changing needs.

More on ArrayList in Java :

ArrayLists can be used to store objects of any type, including primitive data types like int, double, etc. To add elements to an ArrayList, you can use the add() method, and to retrieve an element, you can use the get() method. You can also remove elements using the remove() method and iterate over the elements using a for loop or an enhanced for loop.

Steps to Iterate over an ArrayList : 

  1. Create an ArrayList
  2. Add elements to the ArrayList using the add() method
  3. Get the size of the ArrayList using the size() method
  4. Iterate over the elements in the ArrayList using a for loop:
    a. Get the current element using the get() method
    b. Do something with the current element
  5. End the loop

Here’s a pseudo-code of the algorithm :

1. ArrayList myList = new ArrayList
2. myList.add("element1")
   myList.add("element2")
   myList.add("element3")
3. int size = myList.size()
4. for i = 0 to size - 1
     a. String currentElement = myList.get(i)
     b. // Do something with currentElement
5. end for

Example 1 : 

Run
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // Create a new ArrayList
        ArrayList< String> myList = new ArrayList<>();

        // Add some elements to the ArrayList
        myList.add("prepinsta");
        myList.add("prepinsta Prime");
        myList.add("coding");

        // Get the size of the ArrayList
        int size = myList.size();

        // Iterate over the elements in the ArrayList using a for loop
        for (int i = 0; i < size; i++) {
            String currentElement = myList.get(i);
            System.out.println(currentElement);
        }
    }
}

Output :

prepinsta
prepinsta Prime
coding

Example 2 : 

Run
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // Create a new ArrayList
        ArrayList< String> myList = new ArrayList<>();

        // Add some elements to the ArrayList
        myList.add("rishikesh");
        myList.add("ayush");
        myList.add("lakshit");

        // Iterate over the elements in the ArrayList using an enhanced for loop
        for (String currentElement : myList) {
            System.out.println(currentElement);
        }
    }
}

Output :

rishikesh
ayush
lakshit

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