How to make a multidimensional array? [closed]

-3

I just started my studies and until now I know how to create a one-dimensional array, I would like to know how to make one with multiple dimensions. Thank you.

    
asked by Santiago Lozano 13.11.2017 в 17:10
source

2 answers

1

You add an additional bracket for each dimension.

int[] unidimensional=new int[123]; // matriz líneal por ejemplo para guardar sonido
int[][] bidimensional=new int[123][123]; // cuadrada por ejemplo para guardar imágenes
int[][][] tridimensional=new int[123][123][123]; // tridimensional, por ejemplo para guardar modelos 3D
int[][][][] tetradimensional=new int[123][123][123][123]; // tetradimensional

There are very complex ways to define arrays, because each additional bracket actually creates an array of arrays, and you can do complex things like having arrays of different sizes for each array in each dimension.

For example, in an explicit initialization of a two-dimensional array you may notice the use of arrays within arrays:

int[][] multi = new int[][]{
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

This type of initialization is equivalent to first defining an array as

int[][] bidimensional=new int[5][10];

and then go through it to fill it with zeros.

As for the complex shapes, you can for example define only some of the dimensions, for example

int[][] bidimensional=new int[5][];

which would tell us that we have an array of 5 integer arrays of indeterminate moment size and therefore you can put arrays of different sizes by initializing each of them dynamically.

If you had a better idea of what you are thinking about using it, maybe we could recommend the appropriate data structure or how to create the arrangement you need.

    
answered by 13.11.2017 в 17:17
0

Array of a 10-cell dimension

int[] arrayUnaDimension = new int[9];

Two-dimensional array of 10 x 10 cells

int[][] arrayDosDimensiones = new int[9][9];

Three-dimensional array of 10 x 10 x 10 cells

int[][][] arrayDeTresDimensiones = new int[9][9][9];

Three-dimensional array of 10 x 10 x 10 x 10 cells

int[][][][] arrayDeCuatroDimensiones = new int[9][9][9][9];

You can also make arrays of String, float, boolean ... The only thing you have to do is replace where the String int puts, for example.

    
answered by 13.11.2017 в 17:25