ArrayList add() method example

ArrayList add() method is used to add an element in the list. We can add elements of any type in arraylist, but make program behave in more predicatable manner, we should add elements of one certain type only in any goven list instance.

Use generics for compile time type safety while adding the element to arraylist.

1. ArrayList add() syntax

add() method first ensures that there is sufficient space in the arraylist. If list does not have space, then it grows the list by adding more spaces in underlying array. Then it add the element to specific array index.

ArrayList add method implementation is given below.

 public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}
  • Method parameter – The element to be added to this list.
  • Method returns true if element is added.
  • Method throws – no exception is thrown.

2. ArrayList add() example

Java program to add a single element at a time in arraylist using add() method.

2.1. Type-safe arraylist using generics

Always use generics to ensure you add only a certain type of element in a given list.

//ArrayList with generics
ArrayList<String> names = new ArrayList<>();

names.add("alex");
names.add("brian");
names.add("charles");

System.out.println(names);

Program output.

[alex, brian, charles]

2.2. Arraylist without type safety

We can add any type of object in list. This is not recommended.

//ArrayList without generics
ArrayList ages = new ArrayList();

ages.add("1");
ages.add("2");
ages.add(3);
ages.add(new Long(4l));

System.out.println(ages);

Program output.

[1, 2, 3, 4]

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “ArrayList add() method example”

  1. Please tell me why this happened?

    ArrayList<ArrayList> main=new ArrayList<ArrayList>();
    ArrayList sub=new ArrayList();
    
    //main ArrayList 
    System.out.println("Main ArrayList "+main);
    
    //add integer in sub arraylist
    sub.add(4);
    sub.add(5);
    
    main.add(sub);
    
    //main arraylist after adding sub
    System.out.println("after adding sub main arraylist "+main);
    
    //clear sub arraylist
    sub.clear();
    
    //add element in sub arraylist
    sub.add(3);
    sub.add(10);
    
    //add sub in main arraylist
    main.add(sub);
    
    //see what happens
    System.out.println("after adding sub main arraylist "+main);
    System.out.println("The main arraylist not contains [4,5] .It contains [3,10] twice.");
    System.out.println("Then where [4,5] has gone?");
    

    Output:

    Main ArrayList []
    after adding sub main arraylist [[4, 5]]
    after adding sub main arraylist [[3, 10], [3, 10]]
    The main arraylist not contains [4,5] .It contains [3,10] twice.
    Then where [4,5] has gone?

    Reply
    • [4,5] gone where you cleared the array list. You have added the same arraylist twice in main list. Whatever you do with 'sub' will reflect in both places in main list.

      import java.util.ArrayList;
      
      public class ListExample 
      {
      	public static void main(String[] args) 
      	{
      		ArrayList&lt;ArrayList&gt; main = new ArrayList&lt;&gt;();
      		ArrayList sub = new ArrayList();
      		
      		sub.add(3);
      		sub.add(10);
      		
      		main.add(sub);
      		main.add(sub);
      		main.add(sub);
      		
      		System.out.println(&quot;Main ArrayList &quot; + main);
      		
      		sub.clear();
      		
      		sub.add(1);
      		
      		System.out.println(&quot;Main ArrayList &quot; + main);
      	}
      }
      
      Output:
      
      Main ArrayList [[3, 10], [3, 10], [3, 10]]
      Main ArrayList [[1], [1], [1]]
      
      Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.