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 :
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 :
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 :
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
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
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 :
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 :
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 :
This message was edited 7 times. Last update was at 04/10/2010 15:25:45
沒有留言:
張貼留言