public class MyStack2 {
private Object[] obj ={};
private int length =0;
public MyStack2(int size){
obj = new Object[size];
}
public void push(Object o){
if(null == o)
return;
if(length >obj.length){
System.out.println("栈满,退出...");
return;
}
obj[length++]=o;
}
public Object pop(){
Object o = null;
if(null == obj || this.empty()){
System.out.println("Stack is null or Stack is empty!");
return null;
}
int lastIndex = length -1;
o = obj[lastIndex];
obj[lastIndex] = null; //prevent memory leak
length--; //debug 2010-5-21
return o;
}
public boolean empty(){
if(length >0)
return false;
return true;
}
public int size(){
return obj.length;
}
public static void main(String[] args){
String[] str = {"ok","tom","tony"};
MyStack2 ms2 = new MyStack2(str.length);
for(String s:str){
ms2.push(s);
}
System.out.println("push is over!");
while(true){
System.out.println("element-->"+ms2.pop());
if(ms2.empty())
break;
}
System.out.println("game over!");
}
}