0

I'm practicing Java and I got this and I don't know where and how to continue:

  • Creates a matrix with user-defined dimensions.
  • Fills the matrix with increasing values from top to bottom, left to right.
  • Print the status of the matrix.

The result that I have to achieve should look like this 5x5:

0  5  10  15  20
1  6  11  16  21
2  7  12  17  22
3  8  13  18  23
4  9  14  19  24

or this 7x2:

0  7
1  8
2  9
3  10
4  11
5  12
6  13

This is what I done:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int count = 0;

    System.out.print("Insert rows [i] : ");
    int i = in.nextInt();
    in.nextLine();

    System.out.println("Insert columns [j] : ");
    int j = in.nextInt();
    in.nextLine();

    int[][] matrix = new int[i][j];
    for (int k = 0; k < matrix.length; k++) {
        for (int l = 0; l < matrix[i].length; l++) {
            matrix[i][j] = count;
            count++;
        }
    }
}
Fabio
  • 11
  • 2

2 Answers2

0

You can fill such an array using a stream in a stream:

int m = 5;
int n = 4;
int[][] arr = new int[m][n];
IntStream.range(0, m).forEach(i ->
        IntStream.range(0, n).forEach(j ->
                arr[i][j] = i + j * m));
Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
//[0, 5, 10, 15]
//[1, 6, 11, 16]
//[2, 7, 12, 17]
//[3, 8, 13, 18]
//[4, 9, 14, 19]

Or you can use a loop in a loop:

int m = 7;
int n = 2;
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
        arr[i][j] = i + j * m;
    }
}
for (int[] row : arr) {
    System.out.println(Arrays.toString(row));
}
//[0, 7]
//[1, 8]
//[2, 9]
//[3, 10]
//[4, 11]
//[5, 12]
//[6, 13]

See also: Printing a snake pattern using an array

0

It is not difficult if you understand how nested loops work. In the inner loop, you can start with the value equal to the row index and increment the value by no. of rows in each iteration of the inner loop.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = in.nextInt();

        System.out.print("Enter the number of columns: ");
        int cols = in.nextInt();

        int[][] matrix = new int[rows][cols];

        // Fill the array
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0, value = i; j < matrix[i].length; j++, value += rows) {
                matrix[i][j] = value;
            }
        }

        // Display the matrix
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

A sample run:

Enter the number of rows: 5
Enter the number of columns: 5
0   5   10  15  20  
1   6   11  16  21  
2   7   12  17  22  
3   8   13  18  23  
4   9   14  19  24  

Another sample run:

Enter the number of rows: 7
Enter the number of columns: 2
0   7   
1   8   
2   9   
3   10  
4   11  
5   12  
6   13  
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110