Stacks
A stack
is a
last-in, first-out (LIFO)
collection, like a stack of dishes at a buffet table, or a stack of
coins on your desk. You add a dish on top, and that is the first dish
you take off the stack.
The principal methods for adding to and removing from a stack are
Push
and Pop( )
;
Stack
also offers a
Peek( ) method, very much like
Queue. The significant methods and properties for
Stack are shown in Table 9-5.
Table 9-5. Stack methods and properties
|
Method or Property |
Purpose |
|---|---|
Synchronized( ) |
Public
static method that returns a |
Count( ) |
Public
property that gets the number of elements in the
|
IsReadOnly |
Public property that gets a value indicating if the
|
IsSynchronized |
Public property that gets a value indicating if the
|
SyncRoot |
Public property that returns an object that can be used to
synchronize access to the |
Clear( ) |
Removes all objects from the |
Clone( ) |
Creates a shallow copy. |
Contains( ) |
Determines if an element is in the |
CopyTo( ) |
Copies the |
GetEnumerator( ) |
Returns an enumerator for the |
Peek( ) |
Returns the object at the top of the |
Pop( ) |
Removes and returns the object at the top of the
|
Push( ) |
Inserts an object at the top of the |
ToArray( ) |
Copies the elements to a new array. |
The ArrayList, Queue, and
Stack ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access