Add an element to specified index of ArrayList - Java Collection Framework

Java examples for Collection Framework:ArrayList

Description

Add an element to specified index of ArrayList

Demo Code


import java.util.ArrayList;
 
public class Main {
 
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
   /* w  w w. jav a  2 s . c  om*/
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
   
    arrayList.add(1,"INSERTED ELEMENT");
    System.out.println("ArrayList contains...");
    for(int index=0; index < arrayList.size(); index++)
      System.out.println(arrayList.get(index));
   
  }
}
 

Result


Related Tutorials