The Wayback Machine - https://web.archive.org/web/20240604200336/https://www.geeksforgeeks.org/java-util-arraylist-add-method-java/
Open In App

Java.util.ArrayList.add() Method in Java

Last Updated : 21 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Below are the add() methods of ArrayList in Java:

  1. boolean add(Object o) : This method appends the specified element to the end of this list.
    Parameters:    
    object o: The element to be appended to this list.
    Exception: NA




    // Java code to illustrate add(Object o)
    import java.io.*;
    import java.util.ArrayList;
      
    public class ArrayListDemo {
    public static void main(String[] args)
        {
      
            // create an empty array list with an initial capacity
            ArrayList<Integer> arrlist = new ArrayList<Integer>(5);
      
            // use add() method to add elements in the list
            arrlist.add(15);
            arrlist.add(20);
            arrlist.add(25);
      
            // prints all the elements available in list
            for (Integer number : arrlist) {
                System.out.println("Number = " + number);
            }
        }
    }

    
    

    Output:

    Number = 15
    Number = 20
    Number = 25
    
  2. void add(int index, Object element) : This method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).
    Parameters:
    index : The index at which the specified element is to be inserted.
    element : The element to be inserted.
    
    Exception:
    Throws IndexOutOfBoundsException if the specified
    index is out of range (index  size()).




    // Java code to illustrate
    // void add(int index, Object element)
    import java.io.*;
    import java.util.ArrayList;
      
    public class ArrayListDemo {
    public static void main(String[] args)
        {
      
            // create an empty array list with an initial capacity
            ArrayList<Integer> arrlist = new ArrayList<Integer>(5);
      
            // use add() method to add elements in the list
            arrlist.add(10);
            arrlist.add(22);
            arrlist.add(30);
            arrlist.add(40);
      
            // adding element 35 at fourth position
            arrlist.add(3, 35);
      
            // let us print all the elements available in list
            for (Integer number : arrlist) {
                System.out.println("Number = " + number);
            }
        }
    }

    
    

    Output:

    Number = 10
    Number = 22
    Number = 30
    Number = 35
    Number = 40
    


Previous Article
Next Article

Similar Reads

Java.util.ArrayList.addall() method in Java
Below are the addAll() methods of ArrayList in Java: boolean addAll(Collection c) : This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the ope
4 min read
Java.util.Arraylist.indexOf() in Java
The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Syntax : public int IndexOf(Object o) obj : The element to search for. // Java code to demonstrate the working of // indexOf in ArrayList // for ArrayList functions import java.util.Arra
2 min read
ArrayList of ArrayList in Java
We have discussed that an array of ArrayList is not possible without warning. A better idea is to use ArrayList of ArrayList. // Java code to demonstrate the concept of // array of ArrayList import java.util.*; public class Arraylist { public static void main(String[] args) { int n = 3; // Here aList is an ArrayList of ArrayLists ArrayList&lt;Array
1 min read
How to clone an ArrayList to another ArrayList in Java?
The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList. Syntax: public Object clone(); Return Value: This function returns a copy of the instance of Object. Below program illustrate the Java.util.ArrayList.clone() method: Example: Java Code // Java pr
2 min read
Copy Elements of One ArrayList to Another ArrayList in Java
It is the implementation class of List Interface. It allows duplicated objects/elements and as well as maintains the insertion order. You can get the element present inside the ArrayList by the index of it now you need to pass it into the getting (index) method. You can add the elements into ArrayList using the add() method. Syntax: ArrayList Initi
7 min read
How to Add All Items From a Collection to an ArrayList in Java?
Given a Collection with some values, the task is to add all the items of this Collection to an ArrayList in Java. Illustrations: Input: Collection = [1, 2, 3] Output: ArrayList = [1, 2, 3]Input: Collection = [GFG, Geek, GeeksForGeeks] Output: ArrayList = [GFG, Geek, GeeksForGeeks] Approach: Get the Collection whose items are to be added into the Ar
2 min read
How to add selected items from a collection to an ArrayList in Java?
Given a Collection with some values, the task is to add selected the items of this Collection to an ArrayList in Java. Examples: Input: Collection = [1, 2, 3], condition = (item != 2) Output: ArrayList = [1, 3] Input: Collection = [GFG, Geek, GeeksForGeeks], condition = (item != GFG) Output: ArrayList = [Geek, GeeksForGeeks] Approach: Get the Colle
2 min read
How to Add an Element at Particular Index in Java ArrayList?
ArrayList.add() method is used to add an element at particular index in Java ArrayList. Syntax: public void add(int index, Object element) ; Parameters: index -position at which the element has to be inserted. The index is zero-based.element - the element to be inserted at the specified position. Exception: throws IndexOutOfBoundsException which oc
2 min read
How to Copy and Add all List Elements to an Empty ArrayList in Java?
We can copy and add List items in Array List using addAll() method. This method accepts a Collection (Ex. List) as an argument and adds the collection items at the end of its calling Collection (Ex. ArrayList). This method returns a boolean value. addAll() return true if the collection successfully adds else it returns false. We can clone the list
2 min read
How to Add Element in Java ArrayList?
Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class. 1. boolean add(Object element): The element passe
2 min read