ArrayOutOfBoundException运行时错误

时间:2015-02-12 18:09:50

标签: java arrays exception stack

我正在尝试运行此代码,以学习java,但我遇到了运行时错误:

import java.util.*;

public class ArrayStack<E> implements StackInterface<E>{
    private E[] theStack;
    private int capacity;
    public static final int CAPACITY = 1000;
    private int top = -1;

    public ArrayStack() {
        top = -1;       // empty stack
        theStack = (E[])(new Object[10]);  // make room for at least 10 items
    }

    public ArrayStack(int initialCapacity) throws IllegalArgumentException {
    /*The constructor creates a new stack with the specified capacity, and throws an IllegalArgumentException
     if the specified capacity is less than zero.*/
        if (initialCapacity < 1)
            throw new IllegalArgumentException
                  ("initialCapacity must be >= 1");
        theStack = (E[]) new Object [capacity];
        capacity = initialCapacity;
        top = -1;

    }

    public void push(E element) throws NullPointerException,IllegalStateException {
        if (size() == capacity)
            throw new IllegalStateException("Stack is full.");
        if (element == null) {
            throw new NullPointerException("Can't push a null element");
        }

        if (theStack.length == top + 1) {
            theStack = java.util.Arrays.copyOf(theStack, theStack.length * 2);
        } // else, there already is room for one new element
        top++;
        theStack[top] = element;
    }
    public int size() { return top+1; }
    public boolean isEmpty() {
        return (top < 0);
    }
    public E pop() throws EmptyStackException{
        E element;
        if(isEmpty()) {
            throw new EmptyStackException();
        }
        element = theStack[top];
        theStack[--top] = null;
        return element;
    }

    public int depth() {
        return size();
    }

    public int capacity() {
        return capacity;
    }

    public void flip() {
        reverse(theStack, 0, size() -1);
    }

    public void reverse(E[] x, int i, int j){
        if(i<j){
            E tmp = x[i];
            x[i] = x[j];
            x[j] = tmp;
            reverse(x, ++i, --j);
        }
    }

    public void transferTop(ArrayStack<E> s) throws EmptyStackException {
    /*The transferTop method transfers the element from the top of stack s to the top of the current stack (this).*/
        if(isEmpty()) {
            throw new EmptyStackException();
        }
        E x;
        x = s.pop();
        this.push(x);

    }

    public E replaceTop(E element) throws NullPointerException {
    /*The replaceTop method replaces the top element of the stack with the specified element and returns the original top element.*/
        if(element == null) throw new NullPointerException("Can't push a null element");
        theStack[top] = element;
        return theStack[top];
    }

    public String toString() {
        String s;
        s = "[";
        if (size() > 0)
            s += theStack[0];
        if (size() > 1)
            for (int i = 1; i <= size() - 1; i++) {
                s += ", " + theStack[i];
            }
        return s + "]";
    }

    public void status(String op, Object element) {
        System.out.print("------> " + op); // print this operation
        System.out.println(", returns " + element); // what was returned
        System.out.print("result: size = " + size() + ", isEmpty = "
                + isEmpty());
        System.out.println(", stack: " + this); // contents of the stack
    }

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Object o;
        //ArrayStack(int initialCapacity)
        StackInterface<Integer> A = new ArrayStack<Integer>(100);
        A.status("new ArrayStack<Integer> A", null);

        A.push(7);
        A.status("A.push(7)", null);
        o = A.pop();
        A.status("A.pop()", o);
        A.push(9);
        A.status("A.push(9)", null);
        o = A.pop();
        A.status("A.pop()", o);
        ArrayStack<String> B = new ArrayStack<String>();
        B.status("new ArrayStack<String> B", null);
        B.push("Bob");
        B.status("B.push(\"Bob\")", null);
        B.push("Alice");
        B.status("B.push(\"Alice\")", null);
        o = B.pop();
        B.status("B.pop()", o);
        B.push("Eve");
        B.status("B.push(\"Eve\")", null);
    }
}

A.push(7)操作生成了ArrayIndexOutOfBoundsException。我想知道我的push()方法需要改变什么。提前谢谢。

1 个答案:

答案 0 :(得分:1)

theStack = (E[]) new Object [capacity];
capacity = initialCapacity;

您分配一个零大小的数组,因为当它设置为deafult 0而不是正确的值时使用capacity

相关问题