Insert an element to List using ListIterator in Java

Java 8Object Oriented ProgrammingProgramming

Let us first create an ArrayList −

ArrayList < Integer > arrList = new ArrayList < Integer > ();
arrList.add(100);
arrList.add(200);
arrList.add(300);
arrList.add(400);
arrList.add(500);

Now, create a ListIterator from the above ArrayList and insert more elements −

ListIterator < Integer > iterator = arrList.listIterator();
iterator.add(1000);
iterator.add(2000);
iterator.add(3000);

Example

 Live Demo

import java.util.ArrayList;
import java.util.ListIterator;
public class Demo {
   public static void main(String[] args) {
      ArrayList<Integer>arrList = new ArrayList<Integer>();
      arrList.add(100);
      arrList.add(200);
      arrList.add(300);
      arrList.add(400);
      arrList.add(500);
      arrList.add(600);
      arrList.add(700);
      arrList.add(800);
      ListIterator<Integer>iterator = arrList.listIterator();
      iterator.add(1000);
      iterator.add(2000);
      iterator.add(3000);
      for (Integer i: arrList) {
         System.out.println(i);
      }
   }
}

output

1000
2000
3000
100
200
300
400
500
600
700
800
raja
Published on 23-Apr-2019 08:55:31
Advertisements