Java 循环数组队列中的toString方法
我需要创建一个循环数组队列,允许用户“环绕”数组。我的老师检查了排队和出列方法,但我在正确打印队列时遇到了问题Java 循环数组队列中的toString方法,java,arrays,queue,Java,Arrays,Queue,我需要创建一个循环数组队列,允许用户“环绕”数组。我的老师检查了排队和出列方法,但我在正确打印队列时遇到了问题 public class MyArrayQueue<E> implements QueueInterface<E> { public static final int CAPACITY = 10; private int capacity; private E q[]; //array of E private int
public class MyArrayQueue<E> implements QueueInterface<E> {
public static final int CAPACITY = 10;
private int capacity;
private E q[]; //array of E
private int f; //front
private int r; //rear
//constructor
public MyArrayQueue() {
this (CAPACITY);
}
//constructor
public MyArrayQueue(int n) {
capacity = n;
q = (E[]) new Object[capacity];
}
public void enqueue(E obj) throws FullQueueException {
if(size() == capacity - 1) { //cannot hold more than n-1
throw new FullQueueException("Full queue exception.");
}
q[r] = obj; //insert object in end of the queue
r = (r + 1) % capacity; //wrap around r
}
public E dequeue() throws EmptyQueueException {
if (isEmpty())
throw new EmptyQueueException("Empty queue exception.");
E temp = q[f]; //retrieve the front object
q[f] = null; //good programming practice
f = (f + 1) % capacity; //wrap around f
return temp;
}
public E front() throws EmptyQueueException {
if (isEmpty())
throw new EmptyQueueException("Empty queue exception.");
return q[f]; //return the front object without removing it
}
public boolean isEmpty() {
return (f == r);
}
public int size() {
return (capacity - f + r) % capacity;
}
//fix this loop for homework assignment, make it wrap around
public String toString() {
if (isEmpty())
return "[]";
String result = "[";
result += q[f];
for (int i = (f+1) % capacity; i != r; i = (i+1) % capacity) {
result += " " +q[i];
}
return result + "]";
}
} //end class
公共类MyArrayQueue实现QueueInterface{
公共静态最终int容量=10;
私人int能力;
私有E q[];//E的数组
private int f;//前面
private int r;//后
//建造师
公共MyArrayQueue(){
这(能力);
}
//建造师
公共MyArrayQueue(int n){
容量=n;
q=(E[])新对象[容量];
}
公共无效排队(E obj)引发FullQueueException{
如果(size()==容量-1){//不能容纳超过n-1个
抛出新的FullQueueException(“FullQueueException”);
}
q[r]=obj;//在队列末尾插入对象
r=(r+1)%capacity;//环绕r
}
public E dequeue()引发EmptyQueueException{
if(isEmpty())
抛出新的EmptyQueueException(“Empty queue exception”);
E temp=q[f];//检索前面的对象
q[f]=null;//良好的编程实践
f=(f+1)%capacity;//环绕f
返回温度;
}
public E front()引发EmptyQueueException{
if(isEmpty())
抛出新的EmptyQueueException(“Empty queue exception”);
返回q[f];//返回前面的对象而不删除它
}
公共布尔值为空(){
返回值(f==r);
}
公共整数大小(){
返回(容量-f+r)%容量;
}
//为家庭作业修复这个循环,使其环绕
公共字符串toString(){
if(isEmpty())
返回“[]”;
字符串结果=“[”;
结果+=q[f];
对于(int i=(f+1)%capacity;i!=r;i=(i+1)%capacity){
结果+=“”+q[i];
}
返回结果+“]”;
}
}//结束类
这也是我的客户机类。我将toString方法更改为用户Jyr建议的方法,但仍然无法正确打印。在我的客户机类中实现它可能有一些错误
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
MyArrayQueue<String> list = new MyArrayQueue<>();
int capacity;
int CAPACITY = 10;
String q[];
for (int i = 0; i < CAPACITY; i++) {
capacity = i;
q = new String[i];
}
String name;
boolean flag = true;
int num;
while (flag) {
System.out.println();
System.out.println("1 ----- enqueue");
System.out.println("2 ----- dequeue");
System.out.println("3 ----- front");
System.out.println("4 ----- ouput elements in the queue");
System.out.println("5 ----- isEmpty");
System.out.println("6 ----- size");
System.out.println("0 ----- exit");
System.out.println();
System.out.println("Enter a command: ");
num = console.nextInt();
System.out.println();
switch(num) {
case 1:
try {
System.out.println("Enter a word to enqueue: ");
name = console.next();
list.enqueue(name);
}
catch (FullQueueException e) {
System.out.println("Full Queue");
}
break;
case 2:
try {
System.out.println(list.dequeue());
}
catch (EmptyQueueException e){
System.out.println("Empty Queue");
}
break;
case 3:
try {
System.out.println(list.front());
}
catch (EmptyQueueException e) {
System.out.println("Empty Queue");
}
break;
case 4:
System.out.println(list.toString());
break;
case 5:
System.out.println(list.isEmpty());
break;
case 6:
System.out.println(list.size());
break;
case 0:
flag = false;
System.out.println("Thank you for using this program. ");
break;
default:
System.out.println("Invalid input.");
break;
}
}
}
publicstaticvoidmain(字符串[]args){
扫描仪控制台=新扫描仪(System.in);
MyArrayQueue list=新建MyArrayQueue();
国际能力;
国际通行能力=10;
字符串q[];
对于(int i=0;i
我相信你把事情复杂化了。您想要的是从f
(例如,i=f
)开始,然后向上移动一个,但既然我们可以回过头来,我们应该向上移动(i+1)%capacity
。我们一直这样做,直到到达终点(例如,r
)。在进入这个循环之前,我们应该验证队列是否为空(以防止错误)。如果它实际上是空的,我们只需返回一个字符串
,其中只有两个括号。所以你的代码看起来有点像这样
public String toString() {
if (isEmpty()) return "[]";
String result = "[";
for (int i = f; i != r; i = (i+1)%capacity) {
result += q[i] + ", ";
}
return result.substring(0,result.length()-2) + "]";
// a bit dirty but gets the job done
}
或不同的方法(关于括号和逗号的打印)
你到底遇到了什么麻烦?请让你的老师教你,单字符变量名是不好的做法(只有极少数例外)。;-)另外,对于多个连接,请考虑使用<代码> StringBuilder < /代码>。为什么(什么时候)打印不正确?请澄清,并给出一个不符合您要求的测试用例。当我将项目排队到数组中时,如a、b、c,一直到I,它正确地输出所有内容,因为它还没有抛出完全队列异常。但当我想让它环绕并用j等替换a时,它会抛出异常,但不会打印出来显示它环绕。因此,我没有得到[j b c d e f g h i],而是得到[a b c d e f g h i]。您不想显示“环绕”(在打印中)。环绕仅用于填充中的
null
值
public String toString() {
if (isEmpty()) return "[]";
String result = "[";
result += q[f];
for (int i = (f+1)%capacity; i != r; i = (i+1)%capacity) {
result += ", " + q[i];
}
return result + "]";
}