Java ArrayList add element by position

Introduction

To add element by position to ArrayList in Java

arrayList.add(index, object);

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   w ww .  ja v  a2 s . c  om*/
    // 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()); 
    
    System.out.println(al);
    

  } 
}



PreviousNext

Related