Java 作为数组的队列实现
所以我被要求在数组中实现队列,直到我看到在我退出队列并尝试将另一个值放入队列后得到一个有趣的输出,我才有任何问题。。。Java 作为数组的队列实现,java,arrays,methods,queue,Java,Arrays,Methods,Queue,所以我被要求在数组中实现队列,直到我看到在我退出队列并尝试将另一个值放入队列后得到一个有趣的输出,我才有任何问题。。。 例如(输出): 我的代码包含queue、dequeque和resize方法,一切正常,我真的不知道如何修复它了。。。 这是我的代码,如果你想试试的话。谢谢 public class MainQueue { public static void main(String[] args) { int capacity=5; Queue<
例如(输出): 我的代码包含queue、dequeque和resize方法,一切正常,我真的不知道如何修复它了。。。 这是我的代码,如果你想试试的话。谢谢
public class MainQueue {
public static void main(String[] args) {
int capacity=5;
Queue<Integer> queue = new Queue<Integer>(capacity);
queue.enqueue(1);
System.out.println("Queue: "+ queue);
queue.enqueue(2);
System.out.println("Queue: "+ queue);
queue.dequeue();
queue.enqueue(3);
System.out.println("Queue: "+ queue);
queue.enqueue(4);
System.out.println("Queue: "+ queue);
queue.enqueue(5);
System.out.println("Queue: "+ queue);
}
}
公共类主队列{
公共静态void main(字符串[]args){
int容量=5;
队列=新队列(容量);
排队。排队(1);
System.out.println(“队列:+Queue”);
排队。排队(2);
System.out.println(“队列:+Queue”);
queue.dequeue();
排队。排队(3);
System.out.println(“队列:+Queue”);
排队。排队(4);
System.out.println(“队列:+Queue”);
排队。排队(5);
System.out.println(“队列:+Queue”);
}
}
阶级
import java.util.NoSuchElementException;
公共类队列{
私有E[]元素;//泛型数组
private int front;//队列的第一个元素或前面
private int back;//队列的最后一个元素或后面
private int capacity;//队列的容量
private int count;//表示队列中当前存储的元素数
公共队列(整数大小)
{
容量=大小;
计数=0;
背面=尺寸-1;
正面=0;
elements=(E[])新对象[size];//队列数组为空
}
//如果队列为空或为false,则返回true
公共布尔值为空()
{
返回计数==0;//表示其为真
}
//向队列中添加元素
公共无效排队(E项)
{
如果(计数=容量)
{
调整大小(容量*2);
//System.out.println(“队列已满”);
}
back=(back+1)%capacity;//示例back=(0+1)%10=1
元素[返回]=项目;
//元素[0]=0
//项目=元素[计数];
计数++;
}
//公共大小调整
公共空间调整大小(整型调整大小){
E[]tmp=(E[])新对象[调整大小];
int电流=前端;
for(int i=0;i计数;i++)
{
tmp[i]=元素[当前];
电流=(电流+1)%count;
}
元素=tmp;
正面=0;
返回=计数-1;
容量=调整大小;
}
//去除头部的出列方法
公共E出列()
{
if(isEmpty())
抛出新的NoSuchElementException(“出列:队列为空”);
其他的
{
计数--;
对于(int x=1;x在dequeue
中,您没有更新back
变量,该变量用于确定使用enqueue
添加新值的位置。此外,在deqque中,当您只有1
个计数时,您可以从索引1
中复制索引0
上不存在的值。这是一种特殊情况,如果您创建了一个容量(大小)为1的队列,将某物排队,然后将其出列,则会产生越界错误。您正在使用当前值back
更新enqueue()方法中的back
,但在dequeu()中没有更新它
方法。这是不正确的。事实上,你应该根据计数计算返回
只要改变一下:
back = (back + 1) % capacity;
致:
为什么您在出列时会减少容量?这在我的机器上按预期工作…我得到[1]、[1,2]、[2,3]、[2,3,4]和[2,3,4,5].这很奇怪,因为我不断得到重复的数字和随机索引中的数字…你在哪里尝试过它?@Huntermillen我确实想过,但即使我去掉它也没有任何区别,因为当我调整容量大小时,不管怎样,是的,我应该去掉它…@Lilith,不要紧。确实如此如果您的初始容量足够首次出列,则不起作用。如果您将容量设置为2,则似乎起作用。谢谢您,先生,您让我开心了!这很有效!:)
import java.util.NoSuchElementException;
public class Queue<E> {
private E[] elements;//array in generic
private int front;//first element or front of the queue
private int back;//last element or back of the queue
private int capacity; //capacity of the queue
private int count; //indicates number of elements currently stored in the queue
public Queue(int size)
{
capacity = size;
count = 0;
back = size-1;
front = 0;
elements =(E []) new Object[size]; //queue array empty
}
//Returns true if the queue is empty or false
public boolean isEmpty()
{
return count==0;//means its true
}
//Add elements to the queue
public void enqueue(E item)
{
if(count == capacity)
{
resize(capacity*2);
// System.out.println("Queue is full");
}
back =(back+1) % capacity; //example back=(0+1)%10=1
elements[back]=item;
//elements[0]=0
//item=elements[count];
count++;
}
//Public resize
public void resize(int reSize){
E[] tmp = (E[]) new Object[reSize];
int current = front;
for (int i = 0; i < count; i++)
{
tmp[i] = elements[current];
current = (current + 1) % count;
}
elements = tmp;
front = 0;
back = count-1;
capacity=reSize;
}
//Dequeue method to remove head
public E dequeue()
{
if(isEmpty())
throw new NoSuchElementException("Dequeue: Queue is empty");
else
{
count--;
for(int x = 1; x <= count; x++)
{
elements[x-1] = elements[x];
}
capacity--;
return (E) elements;
}
}
//peek the first element
public E peek()
{
if(isEmpty())
throw new NoSuchElementException("Peek: Queue is empty");
else
return elements[front];
}
//Print queue as string
public String toString()
{
if(isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
String s = "[";
for(int i = 0; i < count; i++)
{
if(i != 0)
s += ", ";
s = s + elements[i];// [value1,value2,....]
}
s +="]";
return s;
}
public void delete() { //Delete everything
count = 0;
}
}
back = (back + 1) % capacity;
back = count % capacity;