Added in API level 1

Stack

open class Stack<E : Any!> : Vector<E>

The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. 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.

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:

<code>Deque&lt;Integer&gt; stack = new ArrayDeque&lt;Integer&gt;();</code>

Summary

Public constructors

Creates an empty Stack.

Public methods
open Boolean

Tests if this stack is empty.

open E

Looks at the object at the top of this stack without removing it from the stack.

open E
pop()

Removes the object at the top of this stack and returns that object as the value of this function.

open E
push(item: E)

Pushes an item onto the top of this stack.

open Int
search(o: Any!)

Returns the 1-based position where an object is on this stack.

Inherited functions
Boolean add(element: E)

Appends the specified element to the end of this Vector.

Unit add(index: Int, element: E)

Inserts the specified element at the specified position in this Vector. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Boolean addAll(index: Int, elements: Collection<E>)

Inserts all of the elements in the specified Collection into this Vector 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 Vector in the order that they are returned by the specified Collection's iterator.

Boolean addAll(elements: Collection<E>)

Appends all of the elements in the specified Collection to the end of this Vector, 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 Vector, and this Vector is nonempty.)

Unit addElement(obj: E)

Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity.

This method is identical in functionality to the #add(java.lang.Object) method (which is part of the List interface).

Int capacity()

Returns the current capacity of this vector.

Unit clear()

Removes all of the elements from this Vector. The Vector will be empty after this call returns (unless it throws an exception).

Any clone()

Returns a clone of this vector. The copy will contain a reference to a clone of the internal data array, not a reference to the original internal data array of this Vector object.

Boolean contains(element: E?)

Returns true if this vector contains the specified element. More formally, returns true if and only if this vector contains at least one element e such that Objects.equals(o, e).

Boolean containsAll(elements: Collection<E>)

Returns true if this Vector contains all of the elements in the specified Collection.

Unit copyInto(anArray: Array<Any!>)

Copies the components of this vector into the specified array. The item at index k in this vector is copied into component k of anArray.

E elementAt(index: Int)

Returns the component at the specified index.

This method is identical in functionality to the get(int) method (which is part of the List interface).

Enumeration<E> elements()

Returns an enumeration of the components of this vector. The returned Enumeration object will generate all items in this vector. The first item generated is the item at index 0, then the item at index 1, and so on. If the vector is structurally modified while enumerating over the elements then the results of enumerating are undefined.

Unit ensureCapacity(minCapacity: Int)

Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.

If the current capacity of this vector is less than minCapacity, then its capacity is increased by replacing its internal data array, kept in the field elementData, with a larger one. The size of the new data array will be the old size plus capacityIncrement, unless the value of capacityIncrement is less than or equal to zero, in which case the new capacity will be twice the old capacity; but if this new size is still smaller than minCapacity, then the new capacity will be minCapacity.

Boolean equals(other: Any?)

Compares the specified Object with this Vector for equality. Returns true if and only if the specified Object is also a List, both Lists have the same size, and all corresponding pairs of elements in the two Lists are equal. (Two elements e1 and e2 are equal if Objects.equals(e1, e2).) In other words, two Lists are defined to be equal if they contain the same elements in the same order.

E firstElement()

Returns the first component (the item at index 0) of this vector.

Unit forEach(action: Consumer<in E>)

E get(index: Int)

Returns the element at the specified position in this Vector.

Int hashCode()

Returns the hash code value for this Vector.

Int indexOf(o: Any?, index: Int)

Returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found. More formally, returns the lowest index i such that (i >= index && Objects.equals(o, get(i))), or -1 if there is no such index.

Int indexOf(element: E?)

Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element. More formally, returns the lowest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.

Unit insertElementAt(obj: E, index: Int)

Inserts the specified object as a component in this vector at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted upward to have an index one greater than the value it had previously.

The index must be a value greater than or equal to 0 and less than or equal to the current size of the vector. (If the index is equal to the current size of the vector, the new element is appended to the Vector.)

This method is identical in functionality to the add(int, E) method (which is part of the List interface). Note that the add method reverses the order of the parameters, to more closely match array usage.

Boolean isEmpty()

Tests if this vector has no components.

MutableIterator<E> iterator()

Returns an iterator over the elements in this list in proper sequence.

The returned iterator is fail-fast.

E lastElement()

Returns the last component of the vector.

Int lastIndexOf(o: Any?, index: Int)

Returns the index of the last occurrence of the specified element in this vector, searching backwards from index, or returns -1 if the element is not found. More formally, returns the highest index i such that (i <= index && Objects.equals(o, get(i))), or -1 if there is no such index.

Int lastIndexOf(element: E?)

Returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element. More formally, returns the highest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.

MutableListIterator<E> listIterator()

Returns a list iterator over the elements in this list (in proper sequence).

The returned list iterator is fail-fast.

MutableListIterator<E> listIterator(index: Int)

Returns a list iterator over 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 next. An initial call to previous would return the element with the specified index minus one.

The returned list iterator is fail-fast.

Boolean remove(element: E?)

Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that Objects.equals(o, get(i)) (if such an element exists).

Boolean removeAll(elements: Collection<E>)

Removes from this Vector all of its elements that are contained in the specified Collection.

Unit removeAllElements()

Removes all components from this vector and sets its size to zero.

This method is identical in functionality to the #clear method (which is part of the List interface).

E removeAt(index: Int)

Removes the element at the specified position in this Vector. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the Vector.

Boolean removeElement(obj: Any?)

Removes the first (lowest-indexed) occurrence of the argument from this vector. If the object is found in this vector, each component in the vector with an index greater or equal to the object's index is shifted downward to have an index one smaller than the value it had previously.

This method is identical in functionality to the #remove(java.lang.Object) method (which is part of the List interface).

Unit removeElementAt(index: Int)

Deletes the component at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted downward to have an index one smaller than the value it had previously. The size of this vector is decreased by 1.

The index must be a value greater than or equal to 0 and less than the current size of the vector.

This method is identical in functionality to the remove(int) method (which is part of the List interface). Note that the remove method returns the old value that was stored at the specified position.

Boolean removeIf(filter: Predicate<in E>)

Unit removeRange(fromIndex: Int, toIndex: Int)

Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)

Unit replaceAll(operator: UnaryOperator<E>)

Boolean retainAll(elements: Collection<E>)

Retains only the elements in this Vector that are contained in the specified Collection. In other words, removes from this Vector all of its elements that are not contained in the specified Collection.

E set(index: Int, element: E)

Replaces the element at the specified position in this Vector with the specified element.

Unit setElementAt(obj: E, index: Int)

Sets the component at the specified index of this vector to be the specified object. The previous component at that position is discarded.

The index must be a value greater than or equal to 0 and less than the current size of the vector.

This method is identical in functionality to the set(int, E) method (which is part of the List interface). Note that the set method reverses the order of the parameters, to more closely match array usage. Note also that the set method returns the old value that was stored at the specified position.

Unit setSize(newSize: Int)

Sets the size of this vector. If the new size is greater than the current size, new null items are added to the end of the vector. If the new size is less than the current size, all components at index newSize and greater are discarded.

Unit sort(c: Comparator<in E>?)

Spliterator<E> spliterator()

Creates a late-binding and fail-fast Spliterator over the elements in this list.

The Spliterator reports Spliterator.SIZED, Spliterator.SUBSIZED, and Spliterator.ORDERED. Overriding implementations should document the reporting of additional characteristic values.

MutableList<E> subList(fromIndex: Int, toIndex: Int)

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.) The returned List is backed by this List, so changes in the returned List are reflected in this List, and vice-versa. The returned List supports all of the optional List operations supported by this List.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a List can be used as a range operation by operating on a subList view instead of a whole List. For example, the following idiom removes a range of elements from a List:

list.subList(from, to).clear();
  
Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the List returned by this method become undefined if the backing list (i.e., this List) is structurally modified in any way other than via the returned List. (Structural modifications are those that change the size of the List, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

Array<Any!> toArray()

Returns an array containing all of the elements in this Vector in the correct order.

Array<T> toArray(a: Array<T>)

Returns an array containing all of the elements in this Vector in the correct order; the runtime type of the returned array is that of the specified array. If the Vector fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this Vector.

If the Vector fits in the specified array with room to spare (i.e., the array has more elements than the Vector), the element in the array immediately following the end of the Vector is set to null. (This is useful in determining the length of the Vector only if the caller knows that the Vector does not contain any null elements.)

String toString()

Returns a string representation of this Vector, containing the String representation of each element.

Unit trimToSize()

Trims the capacity of this vector to be the vector's current size. If the capacity of this vector is larger than its current size, then the capacity is changed to equal the size by replacing its internal data array, kept in the field elementData, with a smaller one. An application can use this operation to minimize the storage of a vector.

Boolean addAll(elements: Collection<E>)

Adds all of the elements in the specified collection to this collection (optional operation). 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 collection, and this collection is nonempty.) If the specified collection has a defined encounter order, processing of its elements generally occurs in that order.

Boolean contains(element: E?)

Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that Objects.equals(o, e).

Boolean containsAll(elements: Collection<E>)

Returns true if this collection contains all of the elements in the specified collection.

Boolean isEmpty()

Returns true if this collection contains no elements.

Boolean remove(element: E?)

Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that Objects.equals(o, e), if this collection contains one or more such elements. Returns true if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).

Boolean removeAll(elements: Collection<E>)

Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection.

Boolean retainAll(elements: Collection<E>)

Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.

Array<Any!> toArray()

Returns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array's runtime component type is Object.

The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.

Array<T> toArray(a: Array<T>)

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)

If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

String toString()

Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

Inherited properties
Int capacityIncrement

The amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity. If the capacity increment is less than or equal to zero, the capacity of the vector is doubled each time it needs to grow.

Int elementCount

The number of valid components in this Vector object. Components elementData[0] through elementData[elementCount-1] are the actual items.

Array<Any!> elementData

The array buffer into which the components of the vector are stored. The capacity of the vector is the length of this array buffer, and is at least large enough to contain all the vector's elements.

Any array elements following the last element in the Vector are null.

Int size

Returns the number of components in this vector.

Int modCount

The number of times this list has been structurally modified. Structural modifications are those that change the size of the list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.

This field is used by the iterator and list iterator implementation returned by the iterator and listIterator methods. If the value of this field changes unexpectedly, the iterator (or list iterator) will throw a ConcurrentModificationException in response to the next, remove, previous, set or add operations. This provides fail-fast behavior, rather than non-deterministic behavior in the face of concurrent modification during iteration.

Use of this field by subclasses is optional. If a subclass wishes to provide fail-fast iterators (and list iterators), then it merely has to increment this field in its add(int, E) and remove(int) methods (and any other methods that it overrides that result in structural modifications to the list). A single call to add(int, E) or remove(int) must add no more than one to this field, or the iterators (and list iterators) will throw bogus ConcurrentModificationExceptions. If an implementation does not wish to provide fail-fast iterators, this field may be ignored.

Public constructors

Stack

Added in API level 1
Stack()

Creates an empty Stack.

Public methods

empty

Added in API level 1
open fun empty(): Boolean

Tests if this stack is empty.

Return
Boolean true if and only if this stack contains no items; false otherwise.

peek

Added in API level 1
open fun peek(): E

Looks at the object at the top of this stack without removing it from the stack.

Return
E the object at the top of this stack (the last item of the Vector object).
Exceptions
java.util.EmptyStackException if this stack is empty.

pop

Added in API level 1
open fun pop(): E

Removes the object at the top of this stack and returns that object as the value of this function.

Return
E The object at the top of this stack (the last item of the Vector object).
Exceptions
java.util.EmptyStackException if this stack is empty.

push

Added in API level 1
open fun push(item: E): E

Pushes an item onto the top of this stack. This has exactly the same effect as:

addElement(item)

Parameters
item E: the item to be pushed onto this stack.
Return
E the item argument.
Added in API level 1
open fun search(o: Any!): Int

Returns the 1-based position where an object is on this stack. If the object o occurs as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1. The equals method is used to compare o to the items in this stack.

Parameters
o Any!: the desired object.
Return
Int the 1-based position from the top of the stack where the object is located; the return value -1 indicates that the object is not on the stack.