Add or insert an element to ArrayList using ListIterator - Java Collection Framework

Java examples for Collection Framework:ListIterator

Description

Add or insert an element to ArrayList using ListIterator

Demo Code


import java.util.ListIterator;
import java.util.ArrayList;
 
public class Main {
 
  public static void main(String[] args) {
   //from  www.  j a  v a 2  s. c  o  m
    //create an object of ArrayList
    ArrayList aList = new ArrayList();
   
    //Add elements to ArrayList object
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
   
    //Get an object of ListIterator using listIterator() method
    ListIterator listIterator = aList.listIterator();
 
    listIterator.next();
       
    //Add an element
    listIterator.add("Added Element");
   
    System.out.println("After inserting element, ArrayList contains");
    for(int intIndex = 0; intIndex < aList.size(); intIndex++)
      System.out.println(aList.get(intIndex));
   
 
  }
}

Result


Related Tutorials