我正在尝试运行此方法以将通用值 (EltType) 插入到双面队列 (deque) 中,但我不断收到我无法弄清楚的 outOfBoundsException。有人能帮我解决这个问题吗?这只是代码的摘录,但我认为可以从中拼凑起来!
private final int CAPACITY = 10;
private int capacity;
private int end;
private EltType deque[];
public ArrayBasedDeque() {
this.capacity = CAPACITY;
deque = (EltType[]) (new Object[capacity]);
}
public void insertFirst(EltType first) {
if(!isEmpty()) {
EltType[] tempArray;
tempArray = (EltType[]) new Object[CAPACITY+1];
for (int i=0;i<=deque.length;i++) {
tempArray[i+1] = deque[i];
}
deque = tempArray;
}
deque[0] = first;
}
public boolean isEmpty() {
boolean returned;
if (deque.length < 1) {
returned = true;
}else {
returned = false;
}
return returned;
}
错误 :
java.lang.ArrayIndexOutOfBoundsException: 10
at ArrayBasedDeque.insertFirst(ArrayBasedDeque.java:48)
at TestABD.main(TestABD.java:5)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)