Collections

How to add to elements in arraylist java – add(E e)

public class ArrayList extends AbstractList
implements List, RandomAccess, Cloneable, Serializable

public boolean add(E e)
Appends the specified element to the end of this list.
Specified by:
add in interface Collection
Specified by:
add in interface List<E>
Overrides:
add in class AbstractList<E>
Parameters:
e – element to be appended to this list
Returns:
true (as specified by Collection.add(E))

package com.candidjava.arrayList;

import java.util.ArrayList;

public class ArrayListAdd 
{
  public static void main(String[] args) {
    
    ArrayList<String> al=new ArrayList<String>();
    
    // add first element to arraylist
    al.add("candid");    
    System.out.println(al); // print arraylist data

    // add more elements to arraylist
    al.add("mathan");
    al.add("antony");
    System.out.println(al);
    
  }
}

Output

[candid]
[candid, mathan, antony]