Create ArrayList program in java

import java.util.*;

class Arrylist_TechSarvam 
{
    public static void main(String args[]) 
    {
        ArrayList<String> list = new ArrayList<String>(); // Creating arraylist
        
        // Adding objects to the arraylist
        list.add("Rajendra");
        list.add("Mahendra");
        list.add("Raja");
        list.add("TechSarvam");

        // Traversing list through Iterator
        Iterator<String> itr = list.iterator();
        while (itr.hasNext()) 
        {
            System.out.println(itr.next());
        }
    }
}

Step-by-Step Explanation:

1.  Class Declaration:  

class Arrylist_TechSarvam {

A class called Arrylist_TechSarvam is declared. This class will contain the code for operating an ArrayList.

2.  Main Method:    

public static void main(String args[]) {

The main() method is where the entrance point of the Java program can be found. It comprises the code that will be executed whenever the program runs.

3. Creating an ArrayList:

ArrayList<String> list = new ArrayList<String>();

An ArrayList called list is declared. This is of type String, which will hold strings. The ArrayList is defined using the keyword new and the constructor: new ArrayList<String>().

4. Adding Elements to the ArrayList:

list.add(“Rajendra”);

list.add(“Mahendra”);

list.add(“Raja”);

list.add(“TechSarvam”);

  • Four strings (“Rajendra”, “Mahendra”, “Raja”, and “TechSarvam”) are added to the ArrayList using the add() method.
  • After these lines, the list contains four elements: [“Rajendra”, “Mahendra”, “Raja”, “TechSarvam”].

5. Traversing the ArrayList Using Iterator:

Iterator itr = list.iterator();

  • An Iterator object is created by calling list.iterator(). The Iterator lets us move through the ArrayList without having to access it with an index.
  • The Iterator offers methods such as hasNext() (to check if there is another element) and next() (to get the next element).

6. Moving Through the ArrayList:

while (itr.hasNext()) {

       System.out.println(itr.next());

  • Above, the while loop iterates to as long as there are more elements left in ArrayList-i.e., to as long as itr.hasNext() returns true.
  • In the body of the loop, itr.next() returns the next element in the list, and System.out.println() prints it to the console.
  • The Iterator will iterate over the list and print each element one by one until it reaches the end of the list.

Output of the Program:

When the program is run, the following output will be printed:

Rajendra

Mahendra

Raja

TechSarvam

Explanation of the Output:

  • Each string in the ArrayList is printed on a new line.
  • The Iterator iterates from the first element of the list:  “Rajendra” to the subsequent elements in the list: “Mahendra”, “Raja”, and “TechSarvam” until all the elements are iterated through in the list.

Important Points:

1. ArrayList: It is a dynamic array in Java which can store objects of any data type. Here, it holds strings.

2. add(): This method is used to add the elements to the ArrayList.

3. Iterator: An interface in Java, providing methods for traversing the collection. ArrayList uses an iterator() method returning an Iterator object.

4. hasNext(): This returns whether there is a next element.

5. next(): This is a method used to retrieve the next element.

Example ArrayList Traversing

ArrayList<String> list = new ArrayList<String>();

list.add(“Rajendra”);

list.add(“Mahendra”);

Iterator itr = list.iterator();

while (itr.hasNext()) {

    System.out.println(itr.next()); // Prints each element in the ArrayList

}

This program will illustrate how an ArrayList and an Iterator are used to operate on a collection of elements in Java.

Leave a Comment

Your email address will not be published. Required fields are marked *