How to create an ArrayList and add elements to ArrayList - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

How to create an ArrayList and add elements to ArrayList

Demo Code


 
import java.util.ArrayList;
 
public class Main {
 
  public static void main(String[] args) {
   //  ww  w .j  a  v a 2s .c o  m
    ArrayList arrayList = new ArrayList();
   
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
   
    System.out.println("Getting elements of ArrayList");
    System.out.println(arrayList.get(0));
    System.out.println(arrayList.get(1));
    System.out.println(arrayList.get(2));
  }
}

Result


Related Tutorials