Java中如何从随机位置开始遍历矩阵

发布于 2025-01-18 17:14:39 字数 980 浏览 0 评论 0原文

我正在尝试编写一个简单的 Java 程序,它可以从用户指定的矩阵中的任何位置开始遍历矩阵(二维数组)。
我首先声明我的矩阵并用随机数填充它,但我不知道从这里去哪里?我怎样才能从随机位置开始遍历矩阵中的每个单元格?

我只是想要一些基本的东西,而不是非常高级的东西,因为我仍然是 Java 的初学者,任何帮助将不胜感激!

public static void main(String[] args) {
               
    // Initialize Matrix randomly
    int R = 3;
    int C = 3;
        
    int[][] matrix = new int[R][C];
    for (int i = 0; i < matrix.length; i++) {
    
        for (int j = 0; j < matrix.length; j++) {
    
            matrix[i][j] = ((int) (Math.random() * 2));
    
        }
    }
    
    //---------------------------
        
    // Robot Moving Algorithm
    
    int i, j, rows=R, cols=C, m, n;
    int Robot_i=0, Robot_j=0;
        
    if (Robot_i==0) {
        
        for (i=Robot_i; i<rows; i++) {
                
                
                
        }
    }

}

以下是我正在寻找的内容的概述:

8 5 1
7 3 2
6 9 4

预期输出,从第 0 行第 1 列开始:5,1,2,4,9,6,7,8,3

I'm trying to write a simple Java program that can traverse a matrix (2D-Array) starting from any position in the matrix that the user specifies.
I started by declaring my matrix and filling it with random numbers, but I'm not sure where to go from here? How can I be able to traverse every cell in my matrix starting from a random position?

I'm just looking to have something basic and not very advanced because I'm still a beginner in Java, any help would be much appreciated!

public static void main(String[] args) {
               
    // Initialize Matrix randomly
    int R = 3;
    int C = 3;
        
    int[][] matrix = new int[R][C];
    for (int i = 0; i < matrix.length; i++) {
    
        for (int j = 0; j < matrix.length; j++) {
    
            matrix[i][j] = ((int) (Math.random() * 2));
    
        }
    }
    
    //---------------------------
        
    // Robot Moving Algorithm
    
    int i, j, rows=R, cols=C, m, n;
    int Robot_i=0, Robot_j=0;
        
    if (Robot_i==0) {
        
        for (i=Robot_i; i<rows; i++) {
                
                
                
        }
    }

}

Here's an overview of what I'm looking for:

8 5 1
7 3 2
6 9 4

expected output, starting from row 0, col 1: 5,1,2,4,9,6,7,8,3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2025-01-25 17:14:39

假设我们有这个矩阵:

[9, 5, 9]
[1, 0, 3]
[1, 2, 0]

您也可以将其视为数组
[9, 5, 9, 1, 0, 3, 1, 2, 0]

并且您可以将矩阵中的任何坐标映射到数组中相应的索引,例如:
坐标第 2 行、第 1 列处的 2 具有索引 7

这里有 2 种将坐标映射到索引的方法,反之亦然

private static int toIndex(int row, int col, int nbCols) {
    return col + row * nbCols;
}

private static int[] toCoords(int index, int nbCols) {
    return new int[] { index / nbCols, index % nbCols };
}

这是一个打印矩阵的实用方法

private static void printMatrix(int[][] matrix) {
    for (int[] ints : matrix) {
        System.out.println(Arrays.toString(ints));
    }
}

现在使用这些工具:

  • 计算起始索引机器人的
  • 在数组中迭代到末尾(数组的大小为 nbRows * nbCols)。
  • 再次开始从索引 0 迭代到机器人的起始索引。

public static void main(String[] args) {

    // Initialize Matrix randomly
    int nbRows = 3;
    int nbCols = 3;

    int[][] matrix = new int[nbRows][nbCols];

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix.length; j++) {
            matrix[i][j] = ((int) (Math.random() * 10));
        }
    }

    printMatrix(matrix);

    //---------------------------

    // Robot Moving Algorithm

    int robotRow = 0;
    int robotCol = 1;

    int robotStartIndex = toIndex(robotRow, robotCol, nbCols);

    for (int i = robotStartIndex; i < nbCols * nbCols; i++) {
        int[] coords = toCoords(i, nbCols);
        System.out.println(matrix[coords[0]][coords[1]]);
    }

    for (int i = 0; i < robotStartIndex; i++) {
        int[] coords = toCoords(i, nbCols);
        System.out.println(matrix[coords[0]][coords[1]]);
    }

}

打印出:

[9, 5, 9]
[1, 0, 3]
[1, 2, 0]
5
9
1
0
3
1
2
0
9

Let's say we have this matrix:

[9, 5, 9]
[1, 0, 3]
[1, 2, 0]

you can also think of it as an array
[9, 5, 9, 1, 0, 3, 1, 2, 0]

and you can map any coordinates in the matrix to its corresponding index in the array, ex:
the 2 at coordinates row 2, col 1 has index 7

here are 2 methods to map coords to index and vice versa

private static int toIndex(int row, int col, int nbCols) {
    return col + row * nbCols;
}

private static int[] toCoords(int index, int nbCols) {
    return new int[] { index / nbCols, index % nbCols };
}

And here is a utility method to print your matrix

private static void printMatrix(int[][] matrix) {
    for (int[] ints : matrix) {
        System.out.println(Arrays.toString(ints));
    }
}

Now with these tools:

  • Calculate the start index of the robot
  • Iterate in the array to the end (the array has size nbRows * nbCols).
  • Start again iterating from index 0 to the start index of the robot.

public static void main(String[] args) {

    // Initialize Matrix randomly
    int nbRows = 3;
    int nbCols = 3;

    int[][] matrix = new int[nbRows][nbCols];

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix.length; j++) {
            matrix[i][j] = ((int) (Math.random() * 10));
        }
    }

    printMatrix(matrix);

    //---------------------------

    // Robot Moving Algorithm

    int robotRow = 0;
    int robotCol = 1;

    int robotStartIndex = toIndex(robotRow, robotCol, nbCols);

    for (int i = robotStartIndex; i < nbCols * nbCols; i++) {
        int[] coords = toCoords(i, nbCols);
        System.out.println(matrix[coords[0]][coords[1]]);
    }

    for (int i = 0; i < robotStartIndex; i++) {
        int[] coords = toCoords(i, nbCols);
        System.out.println(matrix[coords[0]][coords[1]]);
    }

}

prints out:

[9, 5, 9]
[1, 0, 3]
[1, 2, 0]
5
9
1
0
3
1
2
0
9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文