| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
The Stack class represents a last-in-first-out (LIFO) stack
of objects. The usual
push and
pop
operations are provided, as well as a method to
peek
at the top item on the stack, a method to test for whether the stack
is empty, and a method to
search the stack for an item
and discover how far it is from the top.
When a stack is first created, it contains no items.
Example
Using a
Stack
var stack : Stack = new Stack ( Number );
stack.push( 20 );
try
{
stack.push("20"); // fail, as "20" isn't a Number
}
catch( e : ClassCastException )
{
trace( e );
}
protected var _aStack:Array
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
protected var _oType:Class
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
public function Stack(type:Class = null, content:Array = null)
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Create an empty Stack.
You can pass the type for stack elements as argument
of the constructor. In that case the stack is considered
as typed
Parameters
| type:Class (default = null) — A Class instance used as type for elements
|
| |
| content:Array (default = null) |
Throws
| — ClassCastException — if the class of the specified
elements prevents it from being added to this list.
|
public function add(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Appends the specified element to the end of this stack.
Parameters
| o:Object — element to be appended to this Stack
|
Returns
| Boolean — true if this stack has changed as result
of the call (as per the general contract of Collection.add).
|
Throws
| — ClassCastException — if the class of the specified
element prevents it from being added to this list.
|
See also
Example
Adding elements to an untyped stack
var stack : Stack = new Stack();
stack.add( 50 );
stack.add( "foo" );
trace( stack.add( "foo" ) ); // true, because stack allow one or more entries
// for an element
trace( stack.size() ); // 3
Adding elements to a typed stack
var stack : Stack = new Stack( String );
stack.add( "foo" );
trace( stack.add( "foo" ) ); // true, because stack allow one or more occurrences
// for an element
try
{
stack.add( 50 ); // fail, as 50 is not a string
}
catch( e : ClassCastException )
{
trace ( e );
}
trace( stack.size() ); // 2
public function addAll(c:Collection):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Appends all of the elements in the specified Collection
to the end of this stack, in the order that
they are returned by the specified Collection's Iterator.
The behavior of this operation is undefined if the
specified Collection is modified while the operation
is in progress. (This implies that the behavior of this
call is undefined if the specified Collection is this
stack, and this stack is nonempty.)
The rules which govern collaboration between typed and untyped
Collection are described in the isValidCollection
descrition, all rules described there are supported by the
containsAll method.
Parameters
| c:Collection — elements to be inserted into this stack.
|
Returns
| Boolean — true if this stack changed as a result of the call.
|
Throws
| — ClassCastException — if the class of an element of the specified
collection prevents it from being added to this collection.
|
| |
| — NullPointerException — if the passed in collection is null.
|
See also
public function addAllAt(index:uint, c:Collection):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Inserts all of the elements in the specified Collection into
this stack at the specified position. Shifts the element
currently at that position (if any) and any subsequent
elements to the right (increases their indices). The new
elements will appear in the stack in the order that
they are returned by the specified Collection's iterator.
The rules which govern collaboration between typed and untyped
Collection are described in the isValidCollection
descrition, all rules described there are supported by the
containsAll method.
Parameters
| index:uint — index uint index at which to insert
first element from the specified collection.
|
| |
| c:Collection — c elements to be inserted into this stack.
|
Returns
| Boolean — true if this stack changed as a result of the call.
|
Throws
| — IndexOutOfBoundsException — index is out of range
(index < 0 || index > size()).
|
| |
| — ClassCastException — if the class of an element of
the specified collection prevents it from being added to this collection.
|
| |
| — NullPointerException — if the passed in collection is null.
|
See also
public function addAt(index:uint, o:Object):void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Inserts the specified element at the specified position
in this stack. Shifts the element currently at that
position (if any) and any subsequent elements to the
right (adds one to their indices).
Parameters
| index:uint — uint index at which the specified
element is to be inserted.
|
| |
| o:Object — element to be inserted.
|
Throws
| — IndexOutOfBoundsException — index is out of range
(index < 0 || index > size()).
|
| |
| — ClassCastException — if the class of the specified
element prevents it from being added to this list.
|
See also
public function clear():void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes all of the elements from this Stack.
The Stack will be empty after this call returns
(unless it throws an exception).
public function contains(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this stack contains at least
one occurence of the specified element. Moreformally,
returns true if and only if this stack contains
at least an element e such that o === e.
Parameters
| o:Object — Object whose presence in this stack
is to be tested.
|
Returns
| Boolean — true if this stack contains the specified
element.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this stack
|
public function containsAll(c:Collection):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this Stack contains all of the elements
in the specified Collection.
Parameters
| c:Collection — a collection whose elements will be tested for
containment in this Stack
|
Returns
| Boolean — true if this Stack contains all of the elements
in the specified collection.
|
Throws
| — NullPointerException — if the passed in collection is null.
|
public function get(index:uint):Object
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns the Object stored at the passed-in
index in this stack object.
If the passed-in index is not a valid index
for this stack, the function throw an
IndexOutOfBoundsException exception.
Parameters
| index:uint — uint index of the entry to get.
|
Returns
| Object — Object stored at the specified index
|
Throws
| — IndexOutOfBoundsException — The passed-in
index is not a valid index for this stack
|
public function getType():Class
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Return the current type allowed in the Stack
Returns
| Class — Class used to type checking.
|
public function indexOf(o:Object):int
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Searches for the first occurence of the given argument.
Parameters
Returns
| int — the index of the first occurrence of the object argument
in this stack, that is, the smallest value k
such that (elem === elementData[k]) && (k >= index)
is true; returns -1 if the object is not found.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this stack
|
public function isEmpty():Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this stack contains no elements.
Returns
| Boolean — true if this stack contains no elements.
|
public function isTyped():Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this stack perform a verification
of the type of elements.
Returns
| Boolean — true if this stack perform a verification
of the type of elements.
|
public function isValidCollection(c:Collection):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Verify that the passed-in Collection is a valid
collection for use with the addAll, addAllAt,
removeAll, retainAll and
containsAll methods.
When dealing with typed and untyped collection, the following rules apply :
- Two typed collection, which have the same type, can collaborate each other.
- Two untyped collection can collaborate each other.
- An untyped collection can add, remove, retain or contains any typed collection
of any type without throwing errors.
- A typed collection will always fail when attempting to add, remove, retain
or contains an untyped collection.
If the passed-in Collection is null the method throw a
NullPointerException error.
Parameters
Returns
| Boolean — boolean true if the collection is valid,
either false
|
Throws
| — NullPointerException — If the passed-in collection
is null
|
| |
| — IllegalArgumentException — If the passed-in collection
type is not the same that the current one.
|
See also
public function isValidIndex(index:uint):void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Verify that the passed-in uint index is a
valid index for this Stack. If not, an
IndexOutOfBoundsException exception is
thrown.
Parameters
| index:uint — uint index to verify
|
Throws
| — IndexOutOfBoundsException — The passed-in
index is not a valid index for this stack
|
public function isValidType(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Verify that the passed-in object type match the current
Stack element's type.
In the case that the stack is untyped the function
will always returns true.
In the case that the object's type prevents it to be added
as element for this stack the method will throw
a ClassCastException.
Parameters
| o:Object — Object to verify
|
Returns
| Boolean — true if the object is elligible for this
stack object, either false.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this stack
|
public function iterator():Iterator
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns an iterator over the elements in this list
in proper sequence. The elements are returned according
to the LIFO order of the stack.
This implementation returns a straightforward implementation
of the iterator interface.
Returns
| Iterator —
an iterator over the elements in this list in proper sequence.
|
public function lastIndexOf(o:Object):int
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns the index of the last occurrence of the specified
object in this Stack.
Parameters
| o:Object — o the desired component.
|
Returns
| int — the index of the first occurrence of the object argument
in this stack, that is, the largest value k
such that (elem === elementData[k]) is true;
returns -1 if the object is not found.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this stack
|
public function listIterator(index:uint = 0):ListIterator
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns a list iterator of the elements in this list
(in proper sequence), starting at the specified position
in the list. The specified index indicates the first
element that would be returned by an initial call to
the next method. An initial call to the previous method
would return the element with the specified index minus one.
This implementation returns a straightforward implementation
of the ListIterator interface that extends the implementation
of the Iterator interface returned by the iterator() method.
The ListIterator implementation relies on the backing list's
get(int), set(int, Object),
add(int, Object) and remove(int)
methods.
Parameters
| index:uint (default = 0) — uint index of the first element
to be returned from the list iterator (by
a call to the next method).
|
Returns
| ListIterator —
a list iterator of the elements in this list (in proper sequence),
starting at the specified position in the list.
|
Throws
| — IndexOutOfBoundsException — index is out of range
(index < 0 || index > size()).
|
public function matchType(o:*):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Verify if the passed-in object can be inserted in the
current Stack.
Parameters
Returns
| Boolean — true if the object can be inserted in
the Stack, either false.
|
public function peek():Object
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Looks at the object at the top of this stack
without removing it from the stack.
Returns
| Object — the object at the top of this stack
(the last item of the object).
|
public function pop():Object
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes the object at the top of this stack and returns
that object as the value of this function.
Returns
| Object — The object at the top of this stack
(the last item of the Stack object).
|
public function push(item:Object):Object
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Pushes an item onto the top of this stack.
This has exactly the same effect as:
Parameters
| item:Object — the item to be pushed onto this stack..
|
Returns
| Object — the item argument.
|
See also
public function remove(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes a single instance of the specified element from this
stack, if this stack contains one or more such elements.
Returns true if this stack contained the specified
element (or equivalently, if this collection changed as a result
of the call).
In order to remove all occurences of an element you have to call
the remove method as long as the stack contains an
occurrence of the passed-in element. Typically, the construct to
remove all occurrences of an element should look like that :
while( stack.contains( element ) ) stack.remove( element );
If the current stack object is typed and if the passed-in object's
type prevents it to be added (and then removed) in this stack,
the function throws a ClassCastException.
Parameters
| o:Object — object to be removed from this stack,
if present.
|
Returns
| Boolean — true if the stack contained the
specified element.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this stack
|
Example
Using the
Stack.remove() method with an untyped stack :
var stack : Stack = new Stack();
stack.add ( "foo" );
stack.add ( "foo" );
trace( stack.size() ); // 1
trace( stack.remove( "foo" ) ); // true, the first occurence of 'foo' have
// been removed from the stack
trace( stack.size() ); // 1
trace( stack.remove( "foo" ) ); // true, the passed-in value has always
// an occurence contained in the stack
trace( stack.size() ); // 0
trace( stack.remove( "foo" ) ); // false, as there is no more occurence
// in the stack
Using the
Stack.remove() method with a typed stack :
var stack : Stack = new Stack( String );
stack.add ( "foo" );
trace( stack.size() ); // 1
trace( stack.remove( "foo" ) ); // true, the passed-in value have been removed
// the code below will produce an exception
try
{
stack.remove( 45 ); // fail, as the passed-in value is not of type string
}
catch( e : ClassCastException )
{
trace ( e );
}
public function removeAll(c:Collection):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes from this stack all of its elements that are contained
in the specified collection (optional operation). At the end
of the call there's no occurences of any elements contained
in the passed-in collection.
The rules which govern collaboration between typed and untyped
Collection are described in the isValidCollection
descrition, all rules described there are supported by the
removeAll method.
Parameters
| c:Collection — Collection that defines which elements will be
removed from this stack.
|
Returns
| Boolean — true if this stack changed as a result
of the call.
|
Throws
| — NullPointerException — if the specified collection is
null.
|
| |
| — IllegalArgumentException — If the passed-in collection
type is not the same that the current one.
|
See also
Example
Using the
Stack.removeAll() with untyped stacks
var stack1 : Stack = new Stack();
var stack2 : Stack = new Stack();
stack1.add( 1 );
stack1.add( 2 );
stack1.add( 3 );
stack1.add( 4 );
stack1.add( "foo1" );
stack1.add( "foo2" );
stack1.add( "foo3" );
stack1.add( "foo4" );
stack2.add( 1 );
stack2.add( 3 );
stack2.add( "foo1" );
stack2.add( "foo3" );
trace ( stack1.removeAll ( stack2 ) ) ;// true
// stack1 now contains :
// 2, 4, 'foo2', 'foo4'
public function removeAt(o:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes the element at the specified position in this stack.
Shifts any subsequent elements to the right (subtracts one from
their indices).
Removes a single instance of the specified element from this
stack, if this stack contains one or more such elements.
Returns true if this stack contained the specified
element (or equivalently, if this collection changed as a result
of the call).
In order to remove all occurences of an element you have to call
the remove method as long as the stack contains an
occurrence of the passed-in element. Typically, the construct to
remove all occurrences of an element should look like that :
while( stack.contains( element ) ) stack.remove( element );
If the current stack object is typed and if the passed-in object's
type prevents it to be added (and then removed) in this stack,
the function throws a ClassCastException.
Parameters
| o:Object — object to be removed from this stack,
if present.
|
Returns
| Boolean — true if the stack contained the
specified element.
|
Throws
| — IndexOutOfBoundsException — The passed-in
index is not a valid index for this stack
|
See also
Example
Using the
Stack.removeAt method with an untyped stack
var stack : Stack = new Stack();
stack.add( "foo1" );
stack.add( "foo2" );
stack.add( "foo3" );
trace ( stack.removeAt( 2 ) ); // return true, 'foo3' have been removed
trace ( stack.removeAt( 0 ) ); // return true, 'foo1' have been removed
// and 'foo2' is now at index 0
try
{
stack.removeAt( 1 ); // fail, as stack have only one entry at index 0
}
catch( e : IndexOutOfBoundsException )
{
trace( e );
}
public function retainAll(c:Collection):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Retains only the elements in this stack that are contained
in the specified collection (optional operation). In other words,
removes from this stack all of its elements that are not
contained in the specified collection.
The rules which govern collaboration between typed and untyped Collection
are described in the isValidCollection descrition, all rules described
there are supported by the retainAll method.
Parameters
| c:Collection — Collection that defines which elements this
stack will retain.
|
Returns
| Boolean — true if this collection changed as a result of the
call.
|
Throws
| — NullPointerException — if the specified collection is
null.
|
| |
| — IllegalArgumentException — If the passed-in collection
type is not the same that the current one.
|
See also
Example
Using the
Stack.retainAll() with untyped stacks
var stack1 : Stack = new Stack();
var stack2 : Stack = new Stack();
stack1.add( 1 );
stack1.add( 2 );
stack1.add( 3 );
stack1.add( 4 );
stack1.add( "foo1" );
stack1.add( "foo2" );
stack1.add( "foo3" );
stack1.add( "foo4" );
stack2.add( 1 );
stack2.add( 3 );
stack2.add( "foo1" );
stack2.add( "foo3" );
trace ( stack1.retainAll ( stack2 ) ) ;// true
// stack1 now contains :
// 1, 3, 'foo1', 'foo3'
public function search(o:Object):int
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Searches for the first occurence of the given argument.
Parameters
| o:Object — an object to search in the stack
|
Returns
| int — the index of the first occurrence of the object argument
in this stack, that is, the smallest value k
such that (elem === elementData[k]) && (k >= index)
is true; returns -1 if the object is not found.
|
Throws
| — ClassCastException — If the object's type
prevents it to be added into this stack
|
public function set(index:uint, o:Object):Object
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Insert the passed-in Object in this stack
at the specified index. The method returns
the object previously stored at this index.
If the passed-in index is not a valid index
for this stack, the function throw an
IndexOutOfBoundsException exception.
If the passed-in object's type prevents it to be added
in this stack the function will throw a
ClassCastException.
Parameters
| index:uint — uint index at which insert the
passed-in Object.
|
| |
| o:Object — Object to insert in this stack
|
Returns
| Object — Object previously stored at the specified
index or null if the insertion haven't been
done.
|
Throws
| — IndexOutOfBoundsException — The passed-in
index is not a valid index for this stack
|
| |
| — ClassCastException — If the object's type
prevents it to be added into this stack
|
public function size():uint
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns the number of components in this Stack.
Returns
| uint — the number of components in this Stack.
|
public function subList(fromIndex:uint, toIndex:uint):List
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns a view of the portion of this List between fromIndex,
inclusive, and toIndex, exclusive. (If fromIndex and ToIndex
are equal, the returned List is empty.)
Parameters
| fromIndex:uint — fromIndex low endpoint (inclusive) of the subList.
|
| |
| toIndex:uint — high endpoint (exclusive) of the subList.
|
Returns
| List —
a view of the specified range within this List.
|
Throws
| — IndexOutOfBoundsException — fromIndex or toIndex are
out of range (index < 0 || index > size()).
|
public function toArray():Array
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns an array containing all the elements in this stack.
Obeys the general contract of the Collection.toArray
method.
Returns
| Array — Array containing all of the elements
in this stack.
|
See also
public function toString():String
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns the String representation of
this object.
The function return a string like
com.bourre.collection::Stack<String>
for a typed collection. The string between the <
and > is the name of the type of the collection's
elements. If the collection is an untyped collection
the function will simply return the result of the
PixlibStringifier.stringify call.
Returns
| String — String representation of
this object.
|
LowRA API documentation 2008- 2009
Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1
mer. févr. 25 2009, 9:22 AM GMT+01:00