【发布时间】:2018-10-02 05:42:44
【问题描述】:
我无法显示队列数组中的元素。我能够显示队列数组的详细信息,但不能显示实际元素。如何打印队列数组中的元素?
提前致谢!
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);
【问题讨论】:
-
需要对数组进行迭代,将元素一个一个打印出来。
-
显示的代码不会编译。查看
main-方法:theQ.printArray(A);中的A是什么?没有定义A。您可能想要打印theQ的元素。然后从方法printArray中删除参数int[] A并让循环打印qarray的元素而不是(现在)A。