title: backward list iteration using iterator
body: <p>here its  a code about listiterator. Work properly in forward direction   but when its come to iterate in backward it goes into infinite loop. after every element print i put a plus and at the end list will generate. works fine if backward iteration's not included but problem with backward. why its happening i dont know?</p>
  
  <pre><code>            package iteratordemo;
              import java.util.*;
  
              /**
               *
               * @author New
               */
              public class IteratorDemo {
  
                  /**
                   * @param args the command line arguments
                   */
                  public static void main(String[] args) {
                      // TODO code application logic here
  
  
                  ArrayList&lt;String&gt; al=new ArrayList&lt;String&gt;();
  
                  al.add("a");
                  al.add("b");
                  al.add("c");
                  al.add("d");
                  al.add("e");
  
  
                  System.out.println("original content of al:" +al);
                  Iterator&lt;String&gt; itr=al.iterator(); 
  
                  while(itr.hasNext()){
                     String element=itr.next();
                     System.out.println(element + " ");
                  }
  
                  System.out.println();
  
          /* modify the object being iterated 
  
            */      
                 ListIterator litr=al.listIterator();
                  while(litr.hasNext()){
                     Object element = litr.next();
                     litr.set(element + "+");
                     System.out.println("modified content of al: " );
                     itr=al.iterator();
                     while(itr.hasNext())
                     {
                        element=itr.next();
                        System.out.println(element + " ");
  
                     }
                     System.out.println();
  
  // now display the list backward
  //  while returning in backward direction it goes into infinite loop                 
                     System.out.println("modified list backwards:");
                     while(litr.hasPrevious()){
  
                           element= litr.previous();
                         System.out.println(element + " ");
  
                     }
                     System.out.println();
  
                  }    
                  }
  
              }
  </code></pre>