【问题标题】:Implement a queue of a 3X3 Matrix实现一个 3X3 矩阵的队列
【发布时间】:2020-12-03 17:23:31
【问题描述】:

基本上,我正在尝试实现一个对象队列 考虑 3x3 矩阵将由用户输入,然后接下来我们将在那里推送更多矩阵。队列如下所示。

1 2 3     2 3 4    1 2 3

4 5 6     5 6 7    7 8 0

7 8 9    1 0 8    4 5 6

如果我们弹出

2 3 4     1 2 3

5 6 7     7 8 0

1 0 8     4 5 6

我设法创建了一个矩阵,但无法推送队列

import java.util.*;


public class Matrix {
    

    public static void main(String[] args) {
    
        int [][]arr = new int[3][3];
        Scanner sc = new Scanner(System.in);
        int i,j;
        ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
        
        ArrayList<Integer> row1 = new ArrayList<>();
        for(i=0;i<3;i++) {
                row1.add(sc.nextInt());
                
        }
        list.add(row1);
        
        ArrayList<Integer> row2 = new ArrayList<>();
        for(i=0;i<3;i++) {
            row2.add(sc.nextInt());
            
    }
        list.add(row2);
        
        ArrayList<Integer> row3 = new ArrayList<>();
        for(i=0;i<3;i++) {
            row3.add(sc.nextInt());
            
    }
        list.add(row3);
        
        //for display
        for(i=0;i<list.size();i++) {
                System.out.println(list.get(i)+" ");
            
            
        }
        
    }

}

【问题讨论】:

  • 我没有看到你定义了一个队列。你试过什么?
  • 我不明白该怎么办?

标签: java arraylist queue


【解决方案1】:

好的,让我们看看我能不能提供一点帮助。首先,在您的代码中,您定义了 int[][] arr,但您不使用它。所以我会摆脱它。

Queue 是另一个容器类,例如 ArrayList,您已经在使用它。所以你需要做的是定义:

Queue<ArrayList<ArrayList<Integer>>> myQueue = new Queue<ArrayList<ArrayList<Integer>>>();

然后你可以添加它:

myQueue.add(list);

但是,我不知道你的任务为什么要这样做,所以我不确定我是否会引导你走上正确的道路。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多