LISTITERATOR

Introduction of ListIterator


Limitations Of Iterator:

  • By using Iterator Object we can able to iterate the list elements in only forward direction. We can’t read in backward direction.

  • Through Iterator we can able to read and remove the data from list but we can’t add/update list object.

ListIterator:

  • In order to overcome this Iterator object drawbacks, sun people came up with one more interface i.e. named as “ListIterator”.

  • It is sub interface to Iterator interface.

  • Naming Convention
  • Through ListIterator we can able to read the data in forward direction as well as in backward direction.

  • Reading should start in forward direction only. i.e, once forward finish then we can do backward direction reading. Means ……

  • Naming Convention
  • Through ListIterator we can able to read the data,remove the data and even we can add/update the data inside list object.

Important Methods in ListIterator:

Some of methods in ListIterator is as follows

  • hasNext().
  • next()
  • remove()
  • hasPrevious()
  • previous()
  • add(Object)
  • set(Object)
  • nextIndex()
  • previousIndex()

Example 1:

 
						
              package com.sdj.pack1;
              public class ArrayListDemo
              {
                 public void printElement(ArrayList lit)
                 {
                    ListIterator li=lit.listIterator();
                    li.add(60);// Adding new element to List object by using ListIterator
                    while(li.hasNext())// reading data in forward direction
                    {
                       System.out.println(li.next());
                    }
                    System.out.println("-------");
                    while(li.hasPrevious())// reading data in backward direction
                    {
                       System.out.println(li.previous());
                    }
                 }
                 public static void main(String[] args)
                 {
                    ArrayList list=new ArrayList();
                    list.add(30);
                    list.add(40);
                    list.add(50);
                    list.add(3333);
                    list.add("pqr");
                    list.add(2025);
                    list.add(3030);
                    ArrayListDemo l=new ArrayListDemo();
                    l.printElement(list);
                 }
              } 
						

Output:

Reading data in forward direction: 30 40 50 3333 pqr 2025 3030 Reading data in backward direction: 3030 2025 pqr 3333 50 40 30 60 ---> This is element added by ListIterator to List Object.
    Note
    • we can able to add elements to the ArrayList Object through only ListIterator . But not possible by using directly ArrayList Object reference variable , If we try to add by using List object directly i.e. list.add(10); then we will get “ConcurrentModificationException”.

    • If we try to add any element to List object by using ListIterator ,That element will be added before to the current ctrl position.

Another Example:

 
						
                     ArrayList list=new ArrayList();
                     list.add(30);
                     list.add(40);
                     ListIterator li=list.listIterator();
                     list.add(60);// RTE i.e  ConcurrentModificationException :we can’t able to add element by
                     List obj, once ListIterator object is create.
                     while(li.hasNext())
                     {
                        System.out.println(li.next());
                     }						

Output:

     
     

How to Update the List Elements thorugh ListIterator :

We can able to update the List elements values by using set(Object newValue).

Set( Object newVal):

It is one of the important method in ListIterator interface.

  • We can update the element value at current location with new value .
  • It won’t raise request to allocate new memory location, like add() method.
  • It can update the old value with new value in existing memory location only.
  • While executing set() if control is pointing at starting of Iterator or end of the iteration location. Then it will raise IllegalStateException.
  • Since if we want to use set() ,we should use next() at least once before calling set().
  • Note

      We can’t use remove() and set() without using next() method

Example 2:

 
						
               package com.sdj.pack1;
               public class RayList
               {
                  public static void main(String[] args)
                  {
                     ArrayList list=new ArrayList();
                     list.add(10);
                     list.add(20);
                     list.add("60");
                     ListIterator it=list.listIterator();
                     while(it.hasNext())
                     {
                        System.out.println(it.next());// read and preint current value
                        it.set("xyz");//it will replace current value with “xyz” for all elements
                     }
                     while(it.hasPrevious())
                     {
                        System.out.println(it.previous());
                     }
                  }
                }                    

Output:

for Forward reading 10 20 30 for Backward reading xyz xyz xyz
    Note

      set() is replaced all the elements with “xyz” , since from 2nd time onwards even in forward direction also we got outputs as “xyz” only.

For each forloop/Enhanced forLoop:

It is one of the jdk1.5 key feature.

Syntax:

 for(element datatype /super type refVar : any Collection Object)

Example : For(Object obj : list); // in List object default element type is Object. So We need to keep left side data type as Object.

Example 3:

 
						
               package com.sdj.pack1;
               public class EnhancedLoop
               {
                  inti;
                  public static void main(String[] args)
                  {
                     ArrayList list=new ArrayList();
                     list.add(10);
                     list.add(20);
                     list.add(30);
                     list.add(40);
                     for(Object element : list)
                     {
                        System.out.println(element);
                     }
                  }
               }                  

Output:

10 20 30 40
Note
    in List object default element type is Object.So We need to keep left side data type is Object. Above program can be replaced with normal for loop also.