简体   繁体   English

我如何在java中排队?

[英]How can I queue in java?

I want to shift the elements in the array in a queue style.我想以队列样式移动数组中的元素。 I did this code:我做了这个代码:

public class Queue {
    private int[] elements;
    private int size;
    public static final int DefCap = 8;

    public Queue() {
        this(DefCap);
    }

    public Queue(int capacity) {
        elements = new int[capacity];
    }

    public int[] enqueue(int v) {
        if (size >= elements.length) {
            int[] a = new int[elements.length * 2];
            System.arraycopy(elements, 0, a, 0, elements.length);
            elements = a;
        }
        elements[size++] = v;
        return elements;
    }

    public int dequeue() {
        return elements[--size];
    }
    public boolean empty() {
        return size == 0;
    }
    public int getSize() {
        return size;
    }

}

How can I shift the numbers in the array where the next number added pushes the last one?如何移动数组中添加的下一个数字推动最后一个数字的数字? because all it does now is removes the last one added (Stacking).因为它现在所做的只是删除添加的最后一个(堆叠)。

First, remember that it doesn't matter at all how you add or retrieve the elements as long as it appears that the operation reflects that of a queue (ie FIFO ).首先,请记住,您如何添加或检索元素并不重要,只要该操作appears反映了queue的操作(即FIFO )。 The internals of your implementation are of no concern to the user(s).您的实现的内部结构与用户无关。 The easiest method (imo) is to add them normally to the end and "remove" them from the beginning.最简单的方法 (imo) 是将它们正常添加到末尾并从头“删除”它们。

When you add the new element, do it like you are already doing it.当你添加新元素时,就像你已经在做的那样。

When you remove the first element, do it virtually by using an index.当您删除第一个元素时,请使用索引进行虚拟操作。

int nextIdx = 0; // initialize start of queue
...
...
public int next() {
   if (nextIdx < elements.length) {
       return elements[nextIdx--];
   } 
   // indicate an error by throwing an exception
}

At some point you are going to want to reclaim the "non-existent" elements at the beginning of the queue and then reset nextIdx .在某些时候,您会想要回收队列开头的“不存在”元素,然后重置nextIdx You can do this when you need to resize the array.当您需要调整数组大小时,您可以执行此操作。 You can use System.arraycopy and make use of both the value of nextIdx and the new desired new size to resize the array and copy the remaining elements.您可以使用System.arraycopy并利用nextIdx的值和新的所需new size来调整数组大小并复制剩余元素。

Note: In your enqueue method I'm not certain why you want to return the entire element array when you add an element.注意:在您的enqueue方法中,我不确定您为什么要在添加元素时返回整个元素数组。 I would expect something like returning the element just added, a boolean indicating success, or not return anything.我希望返回刚刚添加的元素,指示成功的布尔值,或者不返回任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得数字出现在队列(Java)中的次数? - How can I obtain the number of times a number appears in a queue (Java)? 如何在BFS搜索中限制队列大小〜Java - How can I limit the queue size in BFS search ~ Java 我如何调用 java.util.queue poll() 方法而不删除队列中的元素 - how can i call java.util.queue poll() method and not remove elements in my queue 如何在不更改队列的情况下打印队列的所有值?(JAVA) - How can i print all the values of a queue without changing the queue?(JAVA) 如何在java中实例化一个队列对象? - How do I instantiate a Queue object in java? 对于Java队列/列表,我如何原子地“排队,如果有空闲空间或出队然后入队”? - How can I atomically “enqueue if free space OR dequeue then enqueue” for a Java queue / list? 如何从外部Java应用程序将对象放入MB队列? - How can I put an object into a MB queue from an external Java application? Java:如何在满足特定条件时排队要执行的异步调用? - Java: How can I queue up asynchronous calls to be executed when a certain condition is met? 如何将消息直接发送到 azure 服务总线(java)中的死信队列? - How can I send a message directly into a dead letter queue in azure service bus (java)? Java:如何将一个线程添加到队列中,另一个如何使用线程对其进行操作? - Java: How can I have one thread add to a queue and another operate on it using threads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM