1. Introduction

In Java, ArrayList is a resizable array implementation of the List interface. One of the most common operations performed on an ArrayList is adding elements to it. This can be done using the add() method provided by the ArrayList class, which appends the specified element to the end of the list.

2. Program Steps

1. Create an ArrayList.

2. Use the add() method to add elements to the ArrayList.

3. Print the ArrayList before and after adding elements.

3. Code Program

import java.util.ArrayList;

public class ArrayListAddElement {

    public static void main(String[] args) {

        // Step 1: Create an ArrayList
        ArrayList<String> fruits = new ArrayList<>();

        // Print the ArrayList before adding elements
        System.out.println("ArrayList before adding elements: " + fruits);

        // Step 2: Use the add() method to add elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Step 3: Print the ArrayList after adding elements
        System.out.println("ArrayList after adding elements: " + fruits);
    }
}

Output:

ArrayList before adding elements: []
ArrayList after adding elements: [Apple, Banana, Cherry]

4. Step By Step Explanation

Step 1: An empty ArrayList named fruits is created.

Step 2: The add() method is called on the ArrayList, appending the elements "Apple", "Banana", and "Cherry" to the end of the list in the order they are added.

Step 3: The ArrayList is printed before and after adding elements to demonstrate that the elements have been successfully added to it.