How to print an array in Java using the for cycle?

0

I have to print a 4 * 4 matrix in the following way

2 4 6 8

10 12 14 16

18 20 22 24

26 28 30 32

I already tried this, but nothing. The only thing I need help with is that the first variable, j, reaches 4, with that I think it would be useful. Excuse me, I'm new .

public class HolaMundo {

    public static void main(String[] args) {

         int[][][][] Arreglo = new int[4][4][4][4];

         for(int j =1, k = 0, l = 0, m = 0; j < 4;j++, k++, l++) {
         Arreglo[j][j][j][j]=j *2;
         Arreglo[j][j][j][j]=j *2;
         System.out.println(Arreglo[j][j][j][j]);
         System.out.println(Arreglo[k][k][k][k]);
         System.out.println(Arreglo[l][l][l][l]);
         System.out.println(Arreglo[m][m][m][m]);
         }
    }
}
    
asked by Angel 29.10.2017 в 22:57
source

3 answers

2

Hey, such a friend!

To be able to travel through a matrix you need two cycles, one that traverses the rows and another that traverses the columns.

   C1 C2 C3 C4
F1 1 2 3 4
F2 5 6 7 8
F3 9 10 11 12
F4 13 14 15 16

A cycle will be moving inside all the F and within this cycle you will have another cycle that goes through all the C (columns). When it finishes traversing all the Cs, it passes to the next F and it crosses all the Cs of it. Code:

int[][] r = new int[4][4];
        int cont = 1;
        // Recorre las filas
        for (int i = 0; i < r.length; i++){
            // Recorre las columnas
            for (int j = 0; j < r.length; j++) {
                // Le agrega un valor a la posición [i][j]
                r[i][j] = cont;
                // imprime el valor agregado
                System.out.print(" " +r[i][j]);
                cont++;
            }
            System.out.println();
        }

Result:

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

I hope you serve, greetings!

I advise you to read about multidimensional arrays, about matrices too.

    
answered by 29.10.2017 в 23:24
0
package pruebas;

/**
 *
 * @author Moisés Alcocer
 * @web: https://www.ironwoods.es
 */
public class matriz {

    public static void main( String[] args ) {

        int n = 2;
        int interval = n;

        int size = 4;

        for( int i = 0; i < size; i++ ) {
            for( int j = 0; j < size; j++ ) {

                System.out.print( n + " " );

                n += interval;
            }
            System.out.println();
        }
    }

} //class

The variable n contains the number that will be incremented and printed.

The variable interval , the increase over n .

The variable size contains the "size" of the array.

Changing these three variables you can easily modify your matrix, the first two for the numbers to show, and the last for its size. If for example you now need an 8 * 8 matrix: you change the value from size to 8. If you want the start figure and the interval to be 7, you change the value of n to this number. The idea is that the code allows variations of use with minimal or no modification.

The two loops are the first to travel through the vertical space, hence a line break is printed at the end, the nested loop runs horizontally.

Greetings.

    
answered by 29.10.2017 в 23:30
0

Clarifications

What is the need to use a 4-dimensional array? It is already complicated to handle one of 2 and to handle one of 3 is a headache. I can not imagine how complicated it would be to work with one of 4.

From what I see, what you want to achieve can be done with a 2-dimensional arrangement. Keep in mind that the dimensions in an array do not mean the number of rows they can have, but the number of addresses they use to store the data.

The number of dimensions of an array is defined in the number of brackets assigned to it. an array of one dimension only has a bracket [] , one of two has two brackets [][] , one of three three and one of four ... Well, it would be crazy to work with an array of so many dimensions. Just keep in mind that the more brackets more dimensions.

One-dimensional array

int[] array = new int[3];  
123

This array stores the information in one direction only. That can be displayed graphically in a column or a row.

Two-dimensional array (two-dimensional)

int[] array = new int[3][3];
        c
        o
        l
        u
        m
        n
        a
        s
        ⬇
filas➡ 123
        123
        123

This array stores information in two directions. That can be displayed graphically as a Excel table , which has rows and columns. The number of rows is declared in the first bracket and the number of columns in the second [filas][columnas] . This is just what you need.

A 3-dimensional array would be more complex and would have 3 brackets [][][] , I will not explain how it works, because with one of two dimensions you can achieve what you want. But if you are curious, I leave this link:

How to declare a three-dimensional arrangement in java

Solution

To achieve what you want you have to declare a two-dimensional array of four rows and four columns.

int[][] arreglo = new int[4][4];
  

The names of the variables should always start with lowercase.

To travel through a two-dimensional array you need to use two cycles, one that traverses the rows and another that traverses the columns.

// Recorre las filas
for (int filas = 0; filas < arreglo.length; filas++) { 

    // Recorre las columnas
    for (int columnas = 0; columnas < arreglo[filas].length; columnas++) {
        arreglo[filas][columnas] = (fila+1) * 2;
        System.out.print(arreglo[filas][columnas]);
    }

    // Cada vez que termina de recorrer una fila, das un salto de linea
    System.out.print("\n");
}

That way the array will be printed as you want:

2  4  6  8

10 12 14 16

18 20 22 24

26 28 30 32
    
answered by 30.10.2017 в 14:59