我在显示队列数组中的元素时遇到问题。我能够显示队列数组的详细信息,但不能显示实际元素。如何打印队列数组中的元素?
提前感谢!
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 Queuen");
}
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);
您可以更改printArray
函数,如下所示:
public static void printArray() {
for (int i = 0; i < qarray.length; i++) {
System.out.println(qarray[i]);
}
}
此外,在main()
使用theQ.printArray()
.
您正在main()
传递array A
并且尚未声明它,因此您应该收到编译错误