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

21 Jun, 2020
Below are the add() methods of ArrayList in Java:
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
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

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

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

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

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

This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java - util package
Java-ArrayList
Java-Collections
Java-Functions
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
How to iterate any Map in Java
Interfaces in Java
Initialize an ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Singleton Class in Java
Collections in Java
Set in Java