Write a program in java to demonstrate List Iterator?

import java.util.*;

public class CollectionList

{

      public static void main(String arg[])

      {

            ArrayList ls=new ArrayList();

            ls.add("Vikrant");

            ls.add("Sunny");

            ls.add("Kanir");

            ls.add(1,"Abhinandan");

            ls.add("Kundan");

            System.out.println("Total Number of elements is "+ls.size());

            System.out.println(ls.contains("Sunny"));

            System.out.println(ls.contains("Vikash"));

            Iterator itr=ls.iterator();

            while(itr.hasNext())

            {

                  System.out.println("\t"+itr.next());

            }

            ls.remove("Kanir");

            System.out.println("Total Number of elements is "+ls.size());

            ListIterator it=ls.listIterator(ls.size());

            while(it.hasPrevious())

            {

                  System.out.println("\t"+it.previous());

            }

      }

}

List Iterator.PNG

, , , , ,

Leave a Reply