E
- the type of elements in this listpublic class MyStack<E> extends Object
Constructor and Description |
---|
MyStack() |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all the elements from the Stack.
|
boolean |
contains(Object o)
Returns true if this stack contains the specified element.
|
E |
get(int index)
Returns the element at the specified position in this list.
|
int |
indexOf(Object o)
Returns the index of the first occurrence of the specified element in this stack,
or -1 if this ArrayList does not contain the element.
|
boolean |
isEmpty()
Test if Stack is empty
|
int |
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this stack,
or -1 if this ArrayList does not contain the element.
|
E |
peek()
Looks at the object at the top of this stack without removing it from the stack.
|
E |
pop()
Removes the object at the top of this stack and returns that object as the value of this function.
|
E |
push(E item)
Pushes item onto the top of this stack
|
int |
search(Object o)
Returns the 1-based position where an object is on this stack.
|
int |
size()
Returns the number of items in the stack
|
String |
toString()
String representation of a stack.
|
public E push(E item)
item
- the item to be pushed onto this stackpublic E pop()
EmptyStackException
- if stack is empty when calledpublic E peek()
EmptyStackException
- if stack is empty when calledpublic boolean isEmpty()
public int size()
public void clear()
public int search(Object o)
Example:
s.push(4); s.push(5); s.push(6); s.search(6); // 1 s.search(5); // 2 s.search(4); // 3 s.search(27) // -1
o
- the desired object.public boolean contains(Object o)
o
- element whose presence in this ArrayList is to be testedpublic int indexOf(Object o)
o
- element to search forpublic int lastIndexOf(Object o)
o
- element to search forpublic String toString()
AbstractCollection.toString()
and
the order is from bottom of the stack to the stop.public E get(int index)
index
- index of the element to return. This is the index in the
underlying ArrayList, NOT the position in the stack as defined in the search
function.IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size())