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