background image

1 - 1 

Fundamentals of Java Programming Lab 14.3.7.1 

Copyright 

 2003, Cisco Systems, Inc. 

Lab 14.3.7.1

  

ListIterators

 

 
Estimated Time:
 20 minutes  
 
Learning Objectives:  

•  In this lab activity, the student will use ListIterators to scan through a Collection.

 

 

Description/Scenario: 

•  A collection can be scanned using an iterator. Two iterator interfaces are available in 

the collection framework.  They are the Iterator and its subclass the ListIterator. Use 
the ListIterator with list objects. Iterators provide methods for scanning through any 
collection. In a set, the order is non-deterministic. This means it is not determined by 
the sequence in which it was added, or by some special key value. When using an 
iterator to move over a set, the iteration moves forward (but not backwards) through 
the list elements.  

•  Iterator objects provide for scanning through the list and for adding and removing 

elements from the collection. 

•  A list object also supports a ListIterator, which allows the list to be scanned 

backwards. 

•  Use ListIterators to traverse through a Collection of Point objects created in the 

previous lab14.3.5. 

 
File Management: 

Open BlueJ. Click on Project from the BlueJ main menu and select New. In the New 
Project window and in the Look in: list box select c:\. Now, double click the javacourse 
folder listed in the text window and a different New Project window opens with javacourse 
in the Look in: list box. Now, double click the chap14 folder listed in the text window and a 
different New Project window opens with chap14 in the Look in: list box. Type lab14.3.7.1 
in the File name text box and click on Create to create a lab14.3.7.1 subfolder in the 
chap14 folder. Import the Line class from lab14.3.5. 

 
Tasks: 
 

Step 1  Use of ListIterator 
 

a.  Import the class Line from previous lab. Add another method called 

listIterator(), which returns a ListIterator object that can be used to iterate 

through the collection of Point objects. A collection can be scanned using an iterator. 
There are two iterator interfaces available in the collection framework. The iterator 
and its subclass the ListIterator.  

b.  Use the ListIterator with list objects. When using an iterator to move over a set, the 

iteration moves forward through the list elements. The ListIterator provides additional 
methods for scanning forward and backwards through the collection. 

 

 

Step 2  Testing the program 
 

a.  Add statements to the main method to test the listIterator() method and the 

ListIterator object it returns. 

 
 


Document Outline