gpt4 book ai didi

java - 修改toString循环数组缓冲区

转载 作者:行者123 更新时间:2023-12-01 09:17:50 26 4
gpt4 key购买 nike

我有java代码并且能够想出一个toString,如下代码所示:

       public class CircularArrayQueue<T> implements QueueADT<T>
{
private final int DEFAULT_CAPACITY = 100;
private int front, rear, count;
private T[] queue;

//-----------------------------------------------------------------
// Creates an empty queue using the default capacity.
//-----------------------------------------------------------------
public CircularArrayQueue()
{
front = rear = count = 0;
queue = (T[]) (new Object[DEFAULT_CAPACITY]);
}

//-----------------------------------------------------------------
// Creates an empty queue using the specified capacity.
//-----------------------------------------------------------------
public CircularArrayQueue (int initialCapacity)
{
front = rear = count = 0;
queue = ( (T[])(new Object[initialCapacity]) );
}

//-----------------------------------------------------------------
// Adds the specified element to the rear of the queue, expanding
// the capacity of the queue array if necessary.
//-----------------------------------------------------------------
public void enqueue (T element)
{
if (size() == queue.length)
expandCapacity();

queue[rear] = element;

rear = (rear+1) % queue.length;

count++;
}

//-----------------------------------------------------------------
// Removes the element at the front of the queue and returns a
// reference to it. Throws an EmptyCollectionException if the
// queue is empty.
//-----------------------------------------------------------------
public T dequeue() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("queue");

T result = queue[front];
queue[front] = null;

front = (front+1) % queue.length;

count--;

return result;
}

//-----------------------------------------------------------------
// Returns a reference to the element at the front of the queue.
// The element is not removed from the queue. Throws an
// EmptyCollectionException if the queue is empty.
//-----------------------------------------------------------------
public T first() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("queue");

return queue[front];
}

//-----------------------------------------------------------------
// Returns true if this queue is empty and false otherwise.
//-----------------------------------------------------------------
public boolean isEmpty()
{
return (count == 0);
}

//-----------------------------------------------------------------
// Returns the number of elements currently in this queue.
//-----------------------------------------------------------------
public int size()
{
return count;
}


//-----------------------------------------------------------------
// Returns a string representation of this queue.
//-----------------------------------------------------------------
public String toString()
{
String result = "";
int scan = 0;

while(scan < count)
{
if(queue[scan]!=null)
{
result += queue[scan].toString()+"\n";
}
scan++;
}

return result;

}

//-----------------------------------------------------------------
// Creates a new array to store the contents of the queue with
// twice the capacity of the old one.
//-----------------------------------------------------------------
public void expandCapacity()
{
T[] larger = (T[])(new Object[queue.length *2]);

for(int scan=0; scan < count; scan++)
{
larger[scan] = queue[front];
front=(front+1) % queue.length;
}

front = 0;
rear = count;
queue = larger;
}
}

如何根据队列的这些输出修改我的 toString:

我想从显示汽车数量的初始字符串开始如果队列为空,那么我将返回初始字符串。如果前面位于后面或前面我想为每辆车从头到尾添加一条线然后添加从数组开头到 back-1 的行否则我想在 front 和 back-1 之间为每辆车添加一条线。

这是我当前的 toString() :

public String toString()
{
String result = "";

for (int scan=0; scan < rear; scan++)
result = result + queue[scan].toString() + "\n";

return result;
}

最佳答案

您编写了伪代码,剩下的工作由一些 if/else-if block 完成。

  public String toString()
{
String result = String.format("There are %d items in the queue.", count);
if (count == 0)
{
return result;
}
else if (front >= back)
{
for (int i = front; i < count; i++)
{
result += queue[i].toString() + "\n";
}
for (int i = 0; i < back; i++)
{
result += queue[i].toString() + "\n";
}
}
else
{
for (int i = front; i < back; i++)
{
result += queue[i].toString() + "\n";
}
}
return result;
}

关于java - 修改toString循环数组缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40406510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com