我无法显示队列数组中的元素。我能够显示队列数组的详细信息,但不能显示实际的元素。如何打印队列数组中的元素?
谢谢!
public class Queue {
private int max, front, back, num;
private int [] qarray;
//Constructor
public Queue (int q) {
max = q;
qarray = new int [max];
front = 0; back = -1; num = 0;
}
//Insert
public void insert(int add) {
if(back >= max -1) back = -1;
qarray [++back] = add; num++;
System.out.println("INSERT " + add + " Was Added to the Queue\n");
}
public void display() {
System.out.println("In The Queue: ");
System.out.println("Max: " + max + ". Front Index: " + front + ". Back Index: " + back
+ ". Index's Occupied: " +num + "\n");
}
//PRINT ARRAY METHOD
public static void printArray(int[] A) {
for (int i = 0; i < A.length; i++) {
System.out.println(A[i]);
}
public static void main(String[] args) {
Queue theQ = new Queue(10);
theQ.insert(11);
theQ.insert(15);
theQ.insert(7);
theQ.display();
theQ.printArray(A);
答案 0 :(得分:1)
您可以如下所示更改printArray
函数:
public static void printArray() {
for (int i = 0; i < qarray.length; i++) {
System.out.println(qarray[i]);
}
}
此外,在main()
中使用theQ.printArray()
。
您正在array A
中传递main()
,但尚未声明它,因此您应该遇到编译错误