Java Reference
In-Depth Information
I then made another of my demo stack classes, MyStack , implement the same interface:
public
public class
class MyStack
MyStack < T > implements
implements SimpleStack < T > {
private
private int
int depth = 0 ;
public
public static
static final
final int
int DEFAULT_INITIAL = 10 ;
private
private T [] stack ;
public
public MyStack () {
this
this ( DEFAULT_INITIAL );
}
public
public MyStack ( int
int howBig ) {
iif ( howBig <= 0 ) {
throw
new IllegalArgumentException (
howBig + " must be positive, but was " + howBig );
throw new
}
stack = ( T []) new
new Object [ howBig ];
}
@Override
public
public boolean
boolean empty () {
return
return depth == 0 ;
}
/** push - add an element onto the stack */
@Override
public
public void
void push ( T obj ) {
// Could check capacity and expand
stack [ depth ++] = obj ;
}
/* pop - return and remove the top element */
@Override
public
public T pop () {
-- depth ;
T tmp = stack [ depth ];
stack [ depth ] = null
null ;
return
return tmp ;
}
/** peek - return the top element but don't remove it */
@Override
public
public T peek () {
iif ( depth == 0 ) {
Search WWH ::




Custom Search