程式扎記: [ Data Structures with Java ] Section 12.3 : List Iterators

標籤

2011年2月13日 星期日

[ Data Structures with Java ] Section 12.3 : List Iterators


Preface :
All List collections have a second type of iterator, called a list iterator, which takes advantages of the linear ordering of elements in the collection. A list iterator can traverse the list in either direction and also modify the list. The methods associated with a list iterator are specified in the generic ListIterator interface, which extends Iterator interface. Like a iterator, we must rely on a List collection method to create a list iterator. In this case, two choices are available. The List method listIterator() returns a ListIterator object that references the first element.
ListIterator iter = aList.listInterator();

A second version, listIterator(index), takes an index position as an argument and returns a ListIterator object that references the element at the specified position :
ListIterator iter = aList.listIterator(index);

If index = size(), the iterator points just past the end of the list. This is useful if we intend to scan the list in the reverse direction. The below figure illustrates how listIterator(index) creates two iterators that reference the element at position 1(a) and the position just past the end of the list (b) :


The ListIterator interface extends the Iterator interface, and so it defines the methods hasNext(), next(), and remove(). A list iterator can scan a list in both the forward and backward directions. The methods hasPrevious() and previous() are available for a backward scan. An Iterator object can only remove an element during an iteration.
A ListIterator has list update methods add() and set() that can add a new element and assign an element to have a new value. Follow up examples will demonstrate how to use those two methods.

ListIterator Set Method :
The set() method updates the value of an element during an iteration. This method must be used in tandem with the methods next() (or previous()) because it assigns a new value to the element last returned by one of those extraction methods. In practice, this is how the update process might occur. First, call next() to extract a value, determine what the update should be, and then use set() to assign new updated value. In the accompanying figure, next() extracts the Integer value 17 from the list and set() updates the value to be 12 :


Below is a sample code to demonstrate how we use next() and set() to traverse a list and update all content to have uppercase format :
- Example 12.3 :
  1. public static void exam12_3(){  
  2.     ArrayListBag aList = new ArrayListBag(4);         
  3.     aList.add("iterator");  
  4.     aList.add("next");  
  5.     aList.add("scan");  
  6.     aList.add("list");  
  7.     System.out.println("List(Before): "+aList.toString());  
  8.     ListIterator iter = aList.listIterator();  
  9.     String scanStr;  
  10.     while(iter.hasNext()) {  
  11.         scanStr = iter.next();  
  12.         scanStr = scanStr.toUpperCase();  
  13.         iter.set(scanStr);  
  14.     }  
  15.     System.out.println("List(After): "+aList.toString());  
  16. }  
Output:
List(Before): [iterator, next, scan, list]
List(After): [ITERATOR, NEXT, SCAN, LIST]

Backward Scan of a List :
A list iterator can scan a list in the backward direction. It uses the methods hasPrevious() and previous() to control the scan. The method hasPrevious() returns true if there are list elements remaining when traversing the list in the reverse direction. Put more simply, hasPrevious() is true if the backward scan has not reached the first element. The method previous() returns the value refered by the iterator and then moves the iterator down one position in the list.
The following figure illustrates the action that occurs when calling previous() with an iterator that currently references the third element in the list. The method extracts the Integer value 17 from the list and moves the iterator to the second element :

Below is the sample code to demonstrates how to visit the list in reverse order via using hasPrevious() and previous() methods :
- Example 12.4 :
  1. public static void exam12_4(){  
  2.     ArrayListBag aList = new ArrayListBag(4);  
  3.     aList.add("red");  
  4.     aList.add("black");  
  5.     aList.add("green");  
  6.     aList.add("blue");  
  7.     ListIterator iter = aList.listIterator(aList.size());  
  8.     while(iter.hasPrevious()) System.out.println(iter.previous());  
  9.     System.out.println();  
  10. }  
Output :
blue green black red

ListIterator Add Method :
Up to this point, we have focused on the ability of a list iterator to scan and update a list. The iterator has an additional ability to modify the list by inserting new elements. The list iterator add() method inserts a new element at the position currently referenced by the iterator. We need to be more precise. The insertion occurs immediately before the current list value and after the previous list value. For instance, assume that listIter is positioned so that calling next() will return 2. The following statement inserts 5 between 17 and 2 :
listIter.add(5);

After inserting the element, the iterator still references the same element. However a call to previous() will return the new value 5. Below is sample code to demonstrate how it works :
- Example 12.5 :
  1. public static void exam12_5(){  
  2.     ArrayListBag aList = new ArrayListBag(7);  
  3.     aList.add("NY");  
  4.     aList.add("AL");  
  5.     aList.add("MT");  
  6.     aList.add("MA");  
  7.     System.out.println("ListA: "+aList.toString());  
  8.     ArrayListBag bList = new ArrayListBag(3);  
  9.     bList.add("WI");  
  10.     bList.add("TN");  
  11.     bList.add("NV");  
  12.     System.out.println("ListB: "+bList.toString());  
  13.     ListIterator aIter = aList.listIterator(2), bIter = bList.listIterator();  
  14.     while(bIter.hasNext())  
  15.         aIter.add(bIter.next());  
  16.     System.out.println("Merge List: "+aList.toString());  
  17. }  
Output:
ListA: [NY, AL, MT, MA]
ListB: [WI, TN, NV]
Merge List: [NY, ALWI, TN, NVMT, MA]
This message was edited 7 times. Last update was at 04/10/2010 15:25:45

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!