TestIterator2.java
Select all
/* TestIterator2.java CIS 260 2/16/2005 David Klick Demonstrates use of Iterator and ListIterator objects. */ import java.util.*; public class TestIterator2 { public static void main(String[] args) { String[] names = { "Irene", "Mike", "Jason", "Mike", "Kouame", "Tim", "Dave" }; System.out.println("Iterating through list:"); Iterator iter = Arrays.asList(names).iterator(); while (iter.hasNext()) System.out.println(iter.next()); System.out.println("\nBouncing back from end using a list iterator:"); ListIterator liter = Arrays.asList(names).listIterator(); while (liter.hasNext()) System.out.println(liter.next()); System.out.println("--- bounce ---"); while (liter.hasPrevious()) System.out.println(liter.previous()); } } /* Sample output: Iterating through list: Irene Mike Jason Mike Kouame Tim Dave Bouncing back from end using a list iterator: Irene Mike Jason Mike Kouame Tim Dave --- bounce --- Dave Tim Kouame Mike Jason Mike Irene */