How to Add Element in Java ArrayList?
Last Updated :
03 Apr, 2023
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 passed as a parameter gets inserted at the end of the list.
Declaration:
public boolean add(Object element)
Parameter:
The element will get added to the end of the list.
Return Value:
This method returns a boolean value true
Example:
Input:
list.add("A");
list.add("B");
list.add("C");
Output:
list=[A,B,C]
Input:
list.add(1);
list.add(2);
list.add(3);
list.add(4);
Output:
list=[1,2,3,4]
Implementation of the given method:
Java
// Java program to add elements in
// Array List using add() method
import java.io.*;
import java.util.ArrayList;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
// add method for integer ArrayList
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println(list);
}
}
Time Complexity: O(1)
Adding a new element at the end takes O(1) time on average.
Auxiliary Space: O(1)
As constant extra space is used.
2. void add(int index, Object element)
The specified element gets inserted into the specified index.
Declaration:
public void add(int index, Object element)
Parameter:
- index - the position at which the element has to be inserted
- element - the element to be inserted.
Exception: It throws IndexOutOfBoundsException if index<0||index>size()
Example:
Input:
list.add("A");
list.add("B");
list.add(1,"C");
Output:
list=["A","C","B"]
Input:
list.add(1);
list.add(2);
list.add(1,3);
list.add(2,4);
Output:
list=[1,3,4,2]
Implementation of the given method:
Java
// Java program to add elements
// at the specified index in the list
// using add(index,element) method
import java.io.*;
import java.util.ArrayList;
class GFG {
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
// add method for integer ArrayList
list.add(1);
list.add(2);
// index is zero based
// 3 gets added to the 1st
// position(zero-based)
list.add(1, 3);
// 4 gets added to the 2nd
// position(zero-based)
list.add(2, 4);
System.out.println(list);
}
}
Time Complexity: O(n)
Auxiliary Space: O(1)
As constant extra space is used.
Similar Reads
How to Replace a Element in Java ArrayList? To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the
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. Ex
2 min read
Convert HashSet to a ArrayList in Java ArrayList class is a resizable array, present in java.util package. The difference between an array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can b
4 min read
How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co
2 min read
How Objects Can an ArrayList Hold in Java? ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. In order to understan
3 min read