双面队列问题

发布于 2024-10-16 11:18:40 字数 1391 浏览 2 评论 0原文

我正在尝试运行此方法以将通用值(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

握住你手 2024-10-23 11:18:40

除了有关 <= 的其他答案之外,您还将临时数组大小设置为 CAPACITY+1,该大小始终为 11。您可能的意思是:

tempArray = (EltType[]) new Object[capacity+1];

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:

tempArray = (EltType[]) new Object[capacity+1];
白馒头 2024-10-23 11:18:40
for (int i=0;i<=deque.length;i++) {

应该更改为

for (int i=0;i<deque.length;i++) {

您使用了“小于或等于”,但数组的最后一项的索引为(length-1)。

for (int i=0;i<=deque.length;i++) {

should be changed into

for (int i=0;i<deque.length;i++) {

You used a "smaller than or equal", but the last item of an array has (length-1) for index.

一身骄傲 2024-10-23 11:18:40

正如其他发帖者所提到的,您遇到了“相差一”错误,也称为栅栏错误。

另外,您可以按如下方式简化 isEmpty() 方法:

public boolean isEmpty() {
    return end == 0;
}

我假设当 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:

public boolean isEmpty() {
    return end == 0;
}

I'm assuming that when end is zero, then that means there are no elements in the deque. You shouldn't check deque.length, because that simply tells you how many elements the array can store, not how many are currently in the array.

浅语花开 2024-10-23 11:18:40

你在哪里改变你的容量?它可能不应该是一个常数。添加时大小也不会增加。

Where are you changing your capacity? It probably shouldn't be a constant. The size is also not being incremented when you add.

不交电费瞎发啥光 2024-10-23 11:18:40
    for (int i=0;i<=deque.length;i++) {

您应该使用 <,而不是 <=

    for (int i=0;i<=deque.length;i++) {

You should use <, not <=.

枕梦 2024-10-23 11:18:40

B/c 你使用的是 <=,你的 deque.length 是 10,但是 deque 只有 9 个索引。

 for (int i=0;i<=deque.length;i++) {
          tempArray[i+1] = deque[i]; 
 }

使用<而是在 for 循环中

B/c you are using <=, you go up to deque.length which is 10, but deque only has 9 indices.

 for (int i=0;i<=deque.length;i++) {
          tempArray[i+1] = deque[i]; 
 }

Use < instead in the for loop

生生不灭 2024-10-23 11:18:40

作为旁注:

public boolean isEmpty() {
  boolean returned;
  if (deque.length < 1) {
   returned = true; 
  }else {
   returned = false; 
  }
  return returned;
}

是不是:

public boolean isEmpty() {
  deque.length < 1
}

看起来更简单?

As a side note:

public boolean isEmpty() {
  boolean returned;
  if (deque.length < 1) {
   returned = true; 
  }else {
   returned = false; 
  }
  return returned;
}

Doesn't:

public boolean isEmpty() {
  deque.length < 1
}

look simplier?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文