在java中实现双端队列的问题

发布于 2024-10-16 05:15:45 字数 1154 浏览 1 评论 0原文

抱歉,只是从我在这里提出的问题开始:这里我正在尝试运行此方法从双面队列(deque)中删除通用值(EltType),但我不断收到错误,我调用 insertFirst 两次,并将值“3”插入数组两次,然后,当我运行removeFirst时,它将打印一次“3”,然后打印“Null”。有人能帮我吗?

class ArrayBasedDeque<EltType> {

  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 EltType removeFirst() {
    EltType[] tempArray;
    EltType returned = deque[0];
    tempArray = (EltType[]) new Object[capacity];
      for (int i=1;i<capacity;i++) {
        tempArray[i-1] = deque[i]; 
      }
      deque = tempArray;
    return returned;
  }


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

  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;
  }

}

谢谢 :)

sorry, just following on from the question I had here : here I am trying to run this method to remove a generic value (EltType) from a double sided queue(deque), but I keep getting an error in that, I call insertFirst twice, and insert the value "3" into the array twice, then, when I run removeFirst, it will print out "3" once, and then "Null" thereafter. Would anyone be able to help me out please ?

class ArrayBasedDeque<EltType> {

  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 EltType removeFirst() {
    EltType[] tempArray;
    EltType returned = deque[0];
    tempArray = (EltType[]) new Object[capacity];
      for (int i=1;i<capacity;i++) {
        tempArray[i-1] = deque[i]; 
      }
      deque = tempArray;
    return returned;
  }


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

  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;
  }

}

Thank you :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

划一舟意中人 2024-10-23 05:15:45

最明显的问题是 end 永远不会改变。 isEmpty() 将始终返回 true。现在让我们看看您的 insertFirst() 方法。

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;
}

知道 isEmpty() 无论如何总是返回 true,那么这段代码有什么问题呢?

The big glaring issue is that end never changes. isEmpty() will always return true. Now let's look at your insertFirst() method.

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;
}

Knowing that isEmpty() always returns true no matter what, what is the problem with this piece of code?

笑着哭最痛 2024-10-23 05:15:45

当你删除一个元素时,你也需要更新你的结束指针。

您还应该研究System.arrayCopy()

You need to update your end pointer too when you remove an element.

You should also investigate System.arrayCopy()

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