/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package iteratordemo;

/**
 *
 * @author Matthew
 */
import java.util.*; 
 class IteratorDemo { 
 public static void main(String args[]) { 
 // create an array list 
     ArrayList al = new ArrayList(); 
 // add elements to the array list 
     al.add("A"); 
     al.add("B"); 
     al.add("C"); 
     al.add("D"); 
     al.add("F"); 
     // use iterator to display contents of al 
     System.out.print("Original contents of al: "); 
     Iterator itr = al.iterator(); 
     
     while(itr.hasNext()) {

         Object element = itr.next(); 
         System.out.print(element + " ");

     } 
     System.out.println(); 
     // modify objects being iterated 
     ListIterator listIt = al.listIterator(); 
    while(listIt.hasNext()) {

         Object element = listIt.next(); 
         listIt.set(element + "+");

     } 
     System.out.print("Modified contents of al: "); 
     itr = al.iterator();
     while(itr.hasNext()) {

         Object element = itr.next(); 
         System.out.print(element + " ");

     } 
     System.out.println(); 
     // now, display the list backwards 
     System.out.print("Modified list backwards: "); 
     while(listIt.hasPrevious()) {

         Object element = listIt.previous(); 
         System.out.print(element + " ");

     } 
     System.out.println(); 
     } 
 }