gpt4 book ai didi

java - ArrayOutOfBoundException 运行时错误

转载 作者:行者123 更新时间:2023-12-01 11:55:27 24 4
gpt4 key购买 nike

我正在尝试运行此代码来学习 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() 方法需要更改什么。提前致谢。

最佳答案

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

您分配了一个零大小的数组,因为您在将容量设置为默认0而不是正确的值时使用了它。

关于java - ArrayOutOfBoundException 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28484727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com