用数组实现堆栈(stack)的方法

解决方案 »

  1.   

    使用Stack类啊,自己看看Stack类
      

  2.   

    public class MyStack {
            Object[] obj = new Object[1];
            public void push(Object o) {
                    if (obj[0] != null) { //判断是否为空,数组不为空,数组大小加一,插入
                            Object[] temp = obj; //temp用来临时保存,复制
                            obj = new Object[obj.length + 1];
                            for (int j = 0; j < temp.length; j++) {
                                    obj[j] = temp[j];
                            }
                            temp = null;
                            obj[obj.length - 1] = o; //在末尾插入
                    }
                    else {
                            obj[0] = o; //数组为空,插入到首位
                    }
            }
            public Object pop() {
                    if (obj[0]!=null){ //判断数组非空,取值
                            Object o = obj[obj.length - 1];
                            Object[] temp = obj; //temp用来临时保存,复制
                            obj = new Object[obj.length - 1]; //数组长度减一
                            for (int j = 0; j < temp.length-1; j++) {
                                    obj[j] = temp[j];
                            }
                            temp = null;
                            return o;
                    }                                        
                    else { 
                            return null; //数组为空,不返回
                    }
            }
            public int search(Object o){
                    if(o!=null){
                            for(int i=0;i<obj.length;i++){
                                    if(obj[i].equals(o))return i;                                        
                            }
                            return -1;
                    }
                    else{
                            return -1;
                    }
            }
            public static void main(String[] args) {
                    MyStack myStack = new MyStack();
                    myStack.push(new String("abc"));
                    myStack.push(new String("cba"));
                    myStack.push(new Integer(111));
                    myStack.push(new Integer(222));
                    
                    for (int i = 0; i < myStack.obj.length; i++) {
                            System.out.println(myStack.obj[i]);
                    }
                    System.out.println("**********");
                    Object o1 = myStack.pop();
                    Object o2 = myStack.pop();
                    Object o3 = myStack.pop();
                    
                    System.out.println(o1);
                    System.out.println(o2);
                    System.out.println(o3);
                    System.out.println("**********");
                    for (int i = 0; i < myStack.obj.length; i++) {
                                            System.out.println(myStack.obj[i]);
                    }                
            }
    } 不过数组有大小限制