双面队列问题
我正在尝试运行此方法以将通用值(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)
I am trying to run this method to insert a generic value (EltType) into an double sided queue(deque), but I keep getting an outOfBoundsException that I just can't figure out. Would anyone please be able to help me with this ? This is just an extract from the code, but I think it can be pieced together from this!
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;
}
Error :
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
除了有关
<=
的其他答案之外,您还将临时数组大小设置为 CAPACITY+1,该大小始终为 11。您可能的意思是:In addition to the other answers about
<=
, you're setting the temp array size to CAPACITY+1, which will always be 11. You probably meant:应该更改为
您使用了“小于或等于”,但数组的最后一项的索引为(length-1)。
should be changed into
You used a "smaller than or equal", but the last item of an array has (length-1) for index.
正如其他发帖者所提到的,您遇到了“相差一”错误,也称为栅栏错误。
另外,您可以按如下方式简化
isEmpty()
方法:我假设当
end
为零时,则意味着双端队列中没有元素。您不应该检查 deque.length,因为它只是告诉您数组可以存储多少个元素,而不是数组中当前有多少个元素。As other posters have mentioned, you have an "off-by-one" error, also called a fencepost error.
Also, you can simplify your
isEmpty()
method as follows:I'm assuming that when
end
is zero, then that means there are no elements in the deque. You shouldn't checkdeque.length
, because that simply tells you how many elements the array can store, not how many are currently in the array.你在哪里改变你的容量?它可能不应该是一个常数。添加时大小也不会增加。
Where are you changing your capacity? It probably shouldn't be a constant. The size is also not being incremented when you add.
您应该使用
<
,而不是<=
。You should use
<
, not<=
.B/c 你使用的是 <=,你的 deque.length 是 10,但是 deque 只有 9 个索引。
使用<而是在 for 循环中
B/c you are using <=, you go up to deque.length which is 10, but deque only has 9 indices.
Use < instead in the for loop
作为旁注:
是不是:
看起来更简单?
As a side note:
Doesn't:
look simplier?