Stack
| Kind of class: | public class |
|---|---|
| Package: | com.bourre.collection |
| Inherits from: | none |
| Implements: | |
| Author: | Romain Flacher, Cédric Néhémie |
| Classpath: | com.bourre.collection.Stack |
| File last modified: | Monday, 24 November 2008, 11:36:49 |
of objects. The usual
push and popoperations are provided, as well as a method to peekat the top item on the stack, a method to test for whether the stackis empty, and a method to
search the stack for an itemand discover how far it is from the top.
When a stack is first created, it contains no items.
- 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 );
}
Summary
- Stack (type:Class = null, content:Array = null)
- Create an empty Stack.
- add (o:Object) : Boolean
- Appends the specified element to the end of this stack.
- addAt (index:uint, o:Object) : void
- Inserts the specified element at the specified position
- addAll (c:Collection) : Boolean
- Appends all of the elements in the specified Collection
- addAllAt (index:uint, c:Collection) : Boolean
- Inserts all of the elements in the specified Collection into
- remove (o:Object) : Boolean
- Removes a single instance of the specified element from this
- removeAt (index:uint) : Boolean
- Removes the element at the specified position in this stack.
- removeAll (c:Collection) : Boolean
- Removes from this stack all of its elements that are contained
- retainAll (c:Collection) : Boolean
- Retains only the elements in this stack that are contained
- contains (o:Object) : Boolean
- Returns true if this stack contains at least
- containsAll (c:Collection) : Boolean
- Returns true if this Stack contains all of the elements
- search (o:Object) : int
- Searches for the first occurence of the given argument.
- indexOf (o:Object) : int
- Searches for the first occurence of the given argument.
- lastIndexOf (o:Object) : int
- Returns the index of the last occurrence of the specified
- clear : void
- Removes all of the elements from this Stack.
- iterator : Iterator
- Returns an iterator over the elements in this list
- listIterator (index:uint = 0) : ListIterator
- Returns a list iterator of the elements in this list
- size : uint
- Returns the number of components in this Stack.
- get (index:uint) : Object
- Returns the Object stored at the passed-in
- set (index:uint, o:Object) : Object
- Insert the passed-in Object in this stack
- peek : Object
- Looks at the object at the top of this stack
- pop : Object
- Removes the object at the top of this stack and returns
- push (item:Object) : Object
- Pushes an item onto the top of this stack.
- subList (fromIndex:uint, toIndex:uint) : List
- Returns a view of the portion of this List between fromIndex,
- isValidIndex (index:uint) : void
- Verify that the passed-in uint index is a
- isEmpty : Boolean
- Returns true if this stack contains no elements.
- isValidCollection (c:Collection) : Boolean
- Verify that the passed-in Collection is a valid
- isValidType (o:Object) : Boolean
- Verify that the passed-in object type match the current
- matchType (o:*) : Boolean
- Verify if the passed-in object can be inserted in the
- isTyped : Boolean
- Returns true if this stack perform a verification
- getType : Class
- Return the current type allowed in the Stack
- toArray : Array
- Returns an array containing all the elements in this stack.
- toString : String
- Returns the String representation of
Constructor
Stack
You can pass the type for stack elements as argument
of the constructor. In that case the stack is considered
as typed
Class instance used as type for elements- {VISDOC_LINK_0}
ClassCastException— if the class of the specified
elements prevents it from being added to this list.
Instance methods
add
-
trueif this stack has changed as result
of the call (as per the general contract of Collection.add).
- {VISDOC_LINK_0}
ClassCastException— if the class of the specified
element prevents it from being added to this list.
- Adding elements to an untyped stack
var stack : Stack = new Stack();
stack.add( 50 );
stack.add( "foo" );
trace( stack.add( "foo" ) );
trace( stack.size() );
Adding elements to a typed stack
var stack : Stack = new Stack( String );
stack.add( "foo" );
trace( stack.add( "foo" ) );
try
{
stack.add( 50 );
}
catch( e : ClassCastException )
{
trace ( e );
}
trace( stack.size() );
addAll
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 untypedCollection are described in the isValidCollectiondescrition, all rules described there are supported by thecontainsAll method.
-
trueif this stack changed as a result of the call.
- {VISDOC_LINK_0}
ClassCastException— if the class of an element of the specified
collection prevents it from being added to this collection. - {VISDOC_LINK_1}
NullPointerException— if the passed in collection is null.
addAllAt
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 untypedCollection are described in the isValidCollectiondescrition, all rules described there are supported by thecontainsAll method.
uint index at which to insertfirst element from the specified collection.
-
trueif this stack changed as a result of the call.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— index is out of range
(index < 0 || index > size()). - {VISDOC_LINK_1}
ClassCastException— if the class of an element of
the specified collection prevents it from being added to this collection. - {VISDOC_LINK_2}
NullPointerException— if the passed in collection is null.
addAt
in this stack. Shifts the element currently at that
position (if any) and any subsequent elements to the
right (adds one to their indices).
uint index at which the specifiedelement is to be inserted.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— index is out of range
(index < 0 || index > size()). - {VISDOC_LINK_1}
ClassCastException— if the class of the specified
element prevents it from being added to this list.
clear
Stack. The
Stack will be empty after this call returns(unless it throws an exception).
- {VISDOC_LINK_0}#
NullPointerException— if thenullmethod is
not supported by this collection.
contains
true if this stack contains at leastone occurence of the specified element. Moreformally,
returns
true if and only if this stack containsat least an element
e such that o === e. Object whose presence in this stackis to be tested.
-
trueif this stack contains the specified
element.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this stack
containsAll
Stack contains all of the elementsin the specified Collection.
containment in this
Stack-
trueif thisStackcontains all of the elements
in the specified collection.
- {VISDOC_LINK_0}
NullPointerException— if the passed in collection is null.
get
Object stored at the passed-inindex in this stack object.If the passed-in index is not a valid index
for this stack, the function throw an IndexOutOfBoundsException exception.
uint index of the entry to get.-
Objectstored at the specifiedindex
- {VISDOC_LINK_0}
IndexOutOfBoundsException— The passed-in
index is not a valid index for this stack
getType
Stack -
Classused to type checking.
indexOf
- the index of the first occurrence of the object argument
in this stack, that is, the smallest valueksuch that(elem === elementData[k]) && (k >= index)is true; returns-1if the object is not found.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this stack
isEmpty
true if this stack contains no elements. -
trueif this stack contains no elements.
isTyped
true if this stack perform a verificationof the type of elements.
-
trueif this stack perform a verification
of the type of elements.
isValidCollection
Collection is a validcollection for use with the
addAll, addAllAt,removeAll, retainAll andcontainsAll 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 aNullPointerException error.
Collection to verify- boolean
trueif the collection is valid,
eitherfalse
- {VISDOC_LINK_0}
NullPointerException— If the passed-in collection
isnull - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
isValidIndex
uint index is avalid index for this Stack. If not, an
IndexOutOfBoundsException exception isthrown.
uint<code> index to verify— The passed-in
@throws <code>IndexOutOfBoundsException
index is not a valid index for this stack
isValidType
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.
Object to verify-
trueif the object is elligible for this
stack object, eitherfalse.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this stack
iterator
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.
- an iterator over the elements in this list in proper sequence.
lastIndexOf
object in this
Stack. - the index of the first occurrence of the object argument
in this stack, that is, the largest valueksuch that(elem === elementData[k])is true;
returns-1if the object is not found.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this stack
listIterator
(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'sget(int), set(int, Object), add(int, Object) and remove(int)methods.
uint index of the first elementto be returned from the list iterator (by
a call to the next method).
- a list iterator of the elements in this list (in proper sequence),
starting at the specified position in the list.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— index is out of range
(index < 0 || index > size()).
matchType
current
Stack. -
trueif the object can be inserted in
theStack, eitherfalse.
peek
without removing it from the stack.
- the object at the top of this stack
(the last item of the object).
pop
that object as the value of this function.
- The object at the top of this stack
(the last item of theStackobject).
push
This has exactly the same effect as:
remove
stack, if this stack contains one or more such elements.
Returns
true if this stack contained the specifiedelement (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.
object to be removed from this stack,if present.
-
trueif the stack contained the
specified element.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this stack
- Using the
Stack.remove()method with an untyped stack :
var stack : Stack = new Stack();
stack.add ( "foo" );
stack.add ( "foo" );
trace( stack.size() );
trace( stack.remove( "foo" ) );
trace( stack.size() );
trace( stack.remove( "foo" ) );
trace( stack.size() );
trace( stack.remove( "foo" ) );
Using theStack.remove()method with a typed stack :
var stack : Stack = new Stack( String );
stack.add ( "foo" );
trace( stack.size() );
trace( stack.remove( "foo" ) );
try
{
stack.remove( 45 );
}
catch( e : ClassCastException )
{
trace ( e );
}
removeAll
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 untypedCollection are described in the isValidCollectiondescrition, all rules described there are supported by theremoveAll method.
Collection that defines which elements will beremoved from this stack.
-
trueif this stack changed as a result
of the call.
- {VISDOC_LINK_0}
NullPointerException— if the specified collection is
null. - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
- 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 ) ) ;
removeAt
Shifts any subsequent elements to the right (subtracts one from
their indices).
@copy com.bourre.collection.Stack#remove()
uint index at which to remove an elementfrom the specified
Collection.-
trueif the object have been removed, false otherwise.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— The passed-in
index is not a valid index for this stack
- Using the
Stack.removeAtmethod with an untyped stack
var stack : Stack = new Stack();
stack.add( "foo1" );
stack.add( "foo2" );
stack.add( "foo3" );
trace ( stack.removeAt( 2 ) );
trace ( stack.removeAt( 0 ) );
try
{
stack.removeAt( 1 );
}
catch( e : IndexOutOfBoundsException )
{
trace( e );
}
retainAll
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 Collectionare described in the isValidCollection descrition, all rules described
there are supported by the retainAll method.
Collection that defines which elements thisstack will retain.
-
trueif this collection changed as a result of the
call.
- {VISDOC_LINK_0}
NullPointerException— if the specified collection is
null. - {VISDOC_LINK_1}
IllegalArgumentException— If the passed-in collection
type is not the same that the current one.
- 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 ) ) ;
search
- the index of the first occurrence of the object argument
in this stack, that is, the smallest valueksuch that(elem === elementData[k]) && (k >= index)is true; returns-1if the object is not found.
- {VISDOC_LINK_0}
ClassCastException— If the object's type
prevents it to be added into this stack
set
Object in this stackat the specified
index. The method returnsthe 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.
uint index at which insert thepassed-in
Object.Object to insert in this stack-
Objectpreviously stored at the specified
indexor null if the insertion haven't been
done.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— The passed-in
index is not a valid index for this stack - {VISDOC_LINK_1}
ClassCastException— If the object's type
prevents it to be added into this stack
size
Stack. - the number of components in this
Stack.
subList
inclusive, and toIndex, exclusive. (If fromIndex and ToIndex
are equal, the returned List is empty.)
- a view of the specified range within this List.
- {VISDOC_LINK_0}
IndexOutOfBoundsException— fromIndex or toIndex are
out of range (index < 0 || index > size()).
toArray
Obeys the general contract of the
Collection.toArraymethod. -
Arraycontaining all of the elements
in this stack.
toString
String representation ofthis object.
The function return a string likecom.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 thePixlibStringifier.stringify call.
-
Stringrepresentation of
this object.