'How to traverse a matrix starting from a random position in Java

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bentaye