Package | org.as3collections.iterators |
Class | public class ListIterator |
Inheritance | ListIterator Object |
Implements | IListIterator |
Subclasses | ReadOnlyListIterator |
IList
interface).
ListIterator
allows to traverse the list in either direction.
IMPORTANT:
A ListIterator
has no current element; its cursor position always lies between the element that would be returned by a call to previous()
and the element that would be returned by a call to next()
.
An iterator for a list of length n
has n+1
possible cursor positions, as illustrated by the carets (^) below:
Element(0) Element(1) Element(2) ... Element(n-1)
cursor positions: ^ ^ ^ ^ ^
Note that the remove()
and set()
methods are not defined in terms of the cursor position; they are defined to operate on the last element returned by a call to next()
or previous()
.
For further information do not hesitate to see the examples at the end of the page.
This documentation is partially based in the Java Collections Framework JavaDoc documentation. For further information see Java Collections Framework
Method | Defined By | ||
---|---|---|---|
ListIterator(source:IList, position:int = 0)
Constructor, creates a new ListIterator object. | ListIterator | ||
add(element:*):Boolean
Inserts the specified element into the list (optional operation). | ListIterator | ||
hasNext():Boolean
Returns true if the iteration has more elements. | ListIterator | ||
hasPrevious():Boolean
Returns true if the iteration has more elements when traversing the list in the reverse direction. | ListIterator | ||
next():*
Returns the next element in the iteration. | ListIterator | ||
nextIndex():int
Returns the index of the element that would be returned by a subsequent call to next. | ListIterator | ||
pointer():*
Returns the internal pointer of the iteration. | ListIterator | ||
previous():*
Returns the previous element in the iteration. | ListIterator | ||
previousIndex():int
Returns the index of the element that would be returned by a subsequent call to previous. | ListIterator | ||
remove():void
Removes from the list the last element that was returned by next or previous. | ListIterator | ||
reset():void
Resets the internal pointer of the iterator. | ListIterator | ||
set(element:*):void
Replaces the last element returned by next or previous with the specified element (optional operation). | ListIterator |
ListIterator | () | Constructor |
public function ListIterator(source:IList, position:int = 0)
Constructor, creates a new ListIterator
object.
source:IList — the source ListIterator to iterate over.
| |
position:int (default = 0 ) — indicates the first element that would be returned by an initial call to next . An initial call to previous would return the element with the specified position minus one.
|
ArgumentError — if the source argument is null .
|
add | () | method |
public function add(element:*):Boolean
Inserts the specified element into the list (optional operation). The element is inserted immediately before the next element that would be returned by next
, if any, and after the next element that would be returned by previous
, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next
would be unaffected, and a subsequent call to previous
would return the new element. (This call increases by one the value that would be returned by a call to nextIndex
or previousIndex
.)
Parameters
element:* — the element to add.
|
Boolean — true if the list has changed as a result of the call. Returns false if the list does not permit duplicates and already contains the specified element.
|
ConcurrentModificationError — if the list was changed directly (without using the iterator) during iteration.
|
hasNext | () | method |
public function hasNext():Boolean
Returns true
if the iteration has more elements.
Boolean — true if the iteration has more elements.
|
hasPrevious | () | method |
public function hasPrevious():Boolean
Returns true
if the iteration has more elements when traversing the list in the reverse direction.
Boolean — true if the iteration has more elements when traversing the list in the reverse direction.
|
next | () | method |
public function next():*
Returns the next element in the iteration.
Returns* — the next element in the iteration.
|
NoSuchElementError — if the iteration has no more elements.
| |
ConcurrentModificationError — if the list was changed directly (without using the iterator) during iteration.
|
nextIndex | () | method |
public function nextIndex():int
Returns the index of the element that would be returned by a subsequent call to next
. (Returns list size if the list iterator is at the end of the list.)
int — the index of the element that would be returned by a subsequent call to next , or list size if list iterator is at end of list.
|
pointer | () | method |
public function pointer():*
Returns the internal pointer of the iteration.
In this implementation the pointer is the index (position) of the iteration, typically an int
.
* — the internal pointer of the iteration.
|
previous | () | method |
public function previous():*
Returns the previous element in the iteration.
Returns* — the previous element in the iteration.
|
NoSuchElementError — if the iteration has no previous elements.
| |
ConcurrentModificationError — if the list was changed directly (without using the iterator) during iteration.
|
previousIndex | () | method |
public function previousIndex():int
Returns the index of the element that would be returned by a subsequent call to previous
. (Returns -1 if the list iterator is at the beginning of the list.)
int — the index of the element that would be returned by a subsequent call to previous , or -1 if list iterator is at beginning of list.
|
remove | () | method |
public function remove():void
Removes from the list the last element that was returned by next
or previous
. This call can only be made once per call to next
or previous
. It can be made only if IListIterator.add
has not been called after the last call to next
or previous
.
org.as3coreaddendum.errors:UnsupportedOperationError — if the remove operation is not supported by this iterator.
| |
org.as3coreaddendum.errors:IllegalStateError — if the next method has not yet been called, or the remove method has already been called after the last call to the next method.
| |
ConcurrentModificationError — if the list was changed directly (without using the iterator) during iteration.
|
reset | () | method |
public function reset():void
Resets the internal pointer of the iterator.
set | () | method |
public function set(element:*):void
Replaces the last element returned by next
or previous
with the specified element (optional operation). This call can be made only if neither IListIterator.remove
nor IListIterator.add
have been called after the last call to next
or previous
.
Parameters
element:* — the element with which to replace the last element returned by next or previous .
|
org.as3coreaddendum.errors:UnsupportedOperationError — if the set operation is not supported by this iterator.
| |
org.as3coreaddendum.errors:ClassCastError — if the class of the specified element prevents it from being added to this list.
| |
org.as3coreaddendum.errors:IllegalStateError — if neither next or previous have been called, or remove or add have been called after the last call to next or previous .
|
import org.as3collections.IList; import org.as3collections.IListIterator; import org.as3collections.lists.ArrayList; var list1:IList = new ArrayList([1, 3, 5]); list1 // [1,3,5] var it:IListIterator = list1.listIterator(); var e:int; while (it.hasNext()) { ITERATION N.1 it.pointer() // -1 it.nextIndex() // 0 it.previousIndex() // -1 e = it.next(); e // 1 it.pointer() // 0 it.nextIndex() // 1 it.previousIndex() // 0 ITERATION N.2 it.pointer() // 0 it.nextIndex() // 1 it.previousIndex() // 0 e = it.next(); e // 3 it.pointer() // 1 it.nextIndex() // 2 it.previousIndex() // 1 if (e == 3) { //list1.add(4) // ConcurrentModificationError: During the iteration, the list was changed directly (without use the iterator). it.add(4); list1 // [1,3,4,5] } ITERATION N.3 it.pointer() // 2 it.nextIndex() // 3 it.previousIndex() // 2 e = it.next(); e // 5 it.pointer() // 3 it.nextIndex() // 4 it.previousIndex() // 3 if (e == 5) { it.remove(); list1 // [1,3,4] } }
import org.as3collections.IList; import org.as3collections.IListIterator; import org.as3collections.lists.ArrayList; var list1:IList = new ArrayList([1, 3, 5]); list1 // [1,3,5] var it:IListIterator = list1.listIterator(list1.size()); var e:int; while (it.hasPrevious()) { ITERATION N.1 it.pointer() // 2 it.nextIndex() // 3 it.previousIndex() // 2 e = it.previous(); e // 5 it.pointer() // 1 it.nextIndex() // 2 it.previousIndex() // 1 if (e == 5) { it.remove() list1 // [1,3] } ITERATION N.2 it.pointer() // 1 it.nextIndex() // 2 it.previousIndex() // 1 e = it.previous(); e // 3 it.pointer() // 0 it.nextIndex() // 1 it.previousIndex() // 0 if (e == 3) { //list1.add(4) // ConcurrentModificationError: During the iteration, the list was changed directly (without use the iterator). it.add(4); list1 // [1,4,3] } ITERATION N.3 it.pointer() // 1 it.nextIndex() // 2 it.previousIndex() // 1 e = it.previous(); e // 4 it.pointer() // 0 it.nextIndex() // 1 it.previousIndex() // 0 ITERATION N.4 it.pointer() // 0 it.nextIndex() // 1 it.previousIndex() // 0 e = it.previous(); e // 1 it.pointer() // -1 it.nextIndex() // 0 it.previousIndex() // -1 }