Put zeros at the ends of a java matrix

1

I am creating a method that given a matrix, put the whole frame full of zeros, that is to say for the matrix

1   2  3  4
5   6  7  8
9  10 11 12
13 14 15 16

the exit should be

0  0  0 0
0  6  7 0
0 10 11 0 
0  0  0 0

I have this code:

public static void llenaCeros(int[][] matriz) {
        for (int i = 0; i <1; i++) { //fila 0
            for (int j = 0; j <= ladoh; j++) {
                matriz[i][j]=0;
                System.out.println(matriz[i][j]);
            }
            System.out.println();
        }
    }

and it prints a matrix of a column filled with zeros, but not the rest of the matrix.

    
asked by Alberto Sanz 02.12.2017 в 17:32
source

1 answer

0

Here you have it, it was only validate that you set it to 0 when the index of x is 0 or equal to the size of rows or when the index of y is 0 or equal to the size of columns:

public static void llenaCeros(int[][] matriz) {
        for (int i = 0; i < matriz.length; i++) { //fila 0
            for (int j = 0; j < matriz[i].length; j++) {
                if (i==0 || i == matriz.length-1 ||  j == 0 || j == matriz[i].length -1)
                    matriz[i][j]=0;
                System.out.print(matriz[i][j]+ " ");
            }
            System.out.println();
        }
    }
    
answered by 02.12.2017 / 18:14
source