Java ArrayList add() Method with Example

In the previous article we have discussed about Java Program to Synchronize an ArrayList

In this article we are going to see the use Java ArrayList add() method along with suitable examples.

Java ArrayList add() Method with Example

Let’s see ArrayList add() method with suitable example.

add(Object element):

This java.util.ArrayList.add(Object element) method adds/inserts an element to the ArrayList at the next successive index position. It always returns true as the collection need a return value in the signature when an element is added.

If we don’t insert any element inside the add() method then it will show NullPointerException.

Syntax-

arrayListName(Object element)

Where,

  • arrayListName refers to the name of your ArrayList.
  • Object element refers to the element that will be added into the arrayList.

Example-1: Java ArrayList add(Object element) Method – Example with String Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements
  • Add another new element to the ArrayList using add(Object element).
  • Print the new ArrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<String> arr = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr.add("Hello");
        arr.add("this");
        arr.add("is");
        arr.add("an");
        arr.add("ArrayList");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // Adding a new element to the ArrayList
        arr.add("Thank you");
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList are: "+arr);
    }
}
Output:

The elements of ArrayList are: [Hello, this, is, an, ArrayList]
The new elements of ArrayList are: [Hello, this, is, an, ArrayList, Thank you]

Example-2: Java ArrayList add(Object element) Method – Example with Integer Type ArrayList

Approach:

  • Create a new ArrayList of type Integer.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Add another new element to the ArrayList using add(Object element).
  • Print the new ArrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr.add(2);
        arr.add(52);
        arr.add(13);
        arr.add(17);
        arr.add(1);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // Adding a new element to the ArrayList
        arr.add(2);
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList are: "+arr);
    }
}

Output:

The elements of ArrayList are: [2, 52, 13, 17, 1]
The new elements of ArrayList are: [2, 52, 13, 17, 1, 2]

add(int index, Object element):

This java.util.ArrayList.add(int index, Object element)method adds/inserts an element at a particular index of ArrayList. It always returns true as the collection need a return value in the signature when an element is added.

If we don’t insert any element inside the add() method then it will show NullPointerException.

Syntax-

arrayListName(int index,Object element)

Where,

  • arrayListName refers to the name of your ArrayList.
  • Object element refers to the element that will be added into the arrayList.
  • int index refers to the index position of new element in the ArrayList.

Example-1: Java ArrayList add(int index, Object element) Method – Example with Integer Type ArrayList

Approach:

  • Create a new ArrayList of type Integer.
  • Add integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements
  • Add another new element to the ArrayList to a specific position using add(int index, int element ) method
  • Print the new ArrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr.add(2);
        arr.add(52);
        arr.add(13);
        arr.add(17);
        arr.add(1);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // Adding a new element to the ArrayList
        arr.add(3,2);
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList are: "+arr);
    }
}
Output:

The elements of ArrayList are: [2, 52, 13, 17, 1]
The new elements of ArrayList are: [2, 52, 13, 2, 17, 1]

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: