Java ArrayList class

Introduction

To create ArrayList in Java we can call its constructor as follows:

ArrayList<String> al = new ArrayList<String>(); 

Full source

import java.util.ArrayList; 
 
public class Main { 
  public static void main(String args[]) { 
    // Create an array list. 
    ArrayList<String> al = new ArrayList<String>(); 
     /*from ww w . j  ava  2s .c  o  m*/
    // Add elements to the array list. 
    al.add("SQL"); 
    al.add("Java"); 
    al.add("Javascript"); 
    al.add("CSS"); 
    al.add("HTML"); 
    al.add("Demo2s.com"); 
    al.add(1, "Hi"); 
 
    System.out.println("Size of al after additions: " + 
                       al.size()); 
 
 
  } 
}



PreviousNext

Related