Print a matrix leaving space between the values

-3

I want to print a matrix and I want to leave spaces between the values shown

public class AH2 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    // Declaramos un array de dos dimensiones con un tamaño de 3 en la
    // primera dimensión y diferentes tamaños en la segunda

    int miArray[][] = new int[4][];
    // No especificamos el tamaño de la segunda dimensión
    miArray[0] = new int[4]; // El tamaño de la segunda dimensión es 4
    miArray[1] = new int[4]; // El tamaño de la segunda dimensión es 4
    miArray[2] = new int[4]; // El tamaño de la segunda dimensión es 4
    miArray[3] = new int[4]; // El tamaño de la segunda dimensión es 4

    // Rellenamos el array con datos

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < miArray[i].length; j++)

            miArray[i][j] = i*j;


    }
    // Visualizamos los datos que contiene el array

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < miArray[i].length; j++)

            System.out.print(miArray[i][j] + "  ");
        System.out.println("\n ");

    }


}

}

Thanks

Annex my code , already with the given answer ,

    
asked by Alfonso Hortua 25.02.2018 в 01:45
source

1 answer

0

To print a matrix you have to do a double loop.

To leave a space is simply to concatenate the value you print with a space (+ ""). The greatest complexity is going through the matrix.

By matrix, I understand that it is a 2-dimensional array, say it is (int), of 32-bit integers.

private int[][] matriz;
private int filas = 5, columnas = 6;
// supongamos que ya está inicializada

for(int i=0; i < filas; i++){
    for(int j=0; j < columnas; j++)
        System.out.print(matriz[i][j] + " ");
    System.out.println();
}

This should be enough to print your matrix. It is important that you know loopear on the matrix, is the basis of any operation on it;)

    
answered by 25.02.2018 в 02:25