如何在Java中使用队列?

huangapple go评论66阅读模式
英文:

How can I queue in java?

问题

我想要以队列的方式对数组中的元素进行移位操作。
我已经编写了以下代码:

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

}

如何使数组中的数字在添加下一个数字时将最早添加的数字推出,从而实现移位操作?
因为目前代码所做的只是移除最后添加的数字(类似栈的行为)。

英文:

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).

答案1

得分: 1

首先,要记住,无论您如何添加或检索元素,只要操作看起来反映了“队列”(即“先进先出”)的操作方式,都没有任何问题。对于用户来说,您实现的内部细节并不重要。最简单的方法(依我之见)是将元素正常添加到末尾,并从开头“移除”它们。

当您添加新元素时,可以像您已经在做的那样进行操作。

当您移除第一个元素时,可以通过使用索引来虚拟地执行操作。

int nextIdx = 0; // 初始化队列的起始位置
...
...
public int next() {
   if (nextIdx < elements.length) {
       return elements[nextIdx++];
   } 
   // 通过抛出异常来指示错误
}

在某个时刻,您可能会想要回收队列开头的“不存在”的元素,然后重置nextIdx。当需要调整数组大小时,您可以执行此操作。您可以使用System.arraycopy,利用nextIdx的值和新的期望“新大小”,来调整数组大小并复制其余元素。

注意:在您的enqueue方法中,我不确定为什么在添加元素时要返回整个元素数组。我期望会返回刚刚添加的元素,一个指示成功的布尔值,或者不返回任何内容。

英文:

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 (i.e. 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.

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 &lt; 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. 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.

Note: In your enqueue method I'm not certain why you want to return the entire element array when you add an element. I would expect something like returning the element just added, a boolean indicating success, or not return anything.

huangapple
  • 本文由 发表于 2020年9月9日 21:13:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63812484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定