I have an array. We need to add several other arrays to it in order to get a view of the structure:

char[] name = {{'А','Б'},{'В','Г'}} 

But writing this line of code is not an option. The array is already there, you need to add arrays. How to do it and should there be "[]" or "[] []"? Thanks in advance.

  • if the size of the array is already known, then you can create for example an array char[][] arrays = new char[4][2] and then add elements in a loop - hotfix

1 answer 1

An array in an array is a two-dimensional array. An array in an array in an array is a three-dimensional array, and so on.

You can create two-dimensional arrays in Java with the [] [] construct:

 char[][] array = new char[length_1][length_2]; 

Bypassing such arrays goes in double loop:

 for(int i = 0; i<array.length; i++){ for(int j = 0; j<array[i].length; j++){ System.out.println(array[i][j]); } } 


Appeal to the elements, as can be seen, through double square brackets [row] [column] ([row] [column]).

  • one
    I will add that you can also declare an array somewhere at the beginning of the class and initialize it already in the constructor, thus setting its dimensions directly during the creation of the object. - Aquinary
  • Good, but how, in this case, add elements to two, three, etc. -dimensional arrays. So: - user219696
  • As in a regular array: array [x] [y] [z] study-java.ru/uroki-java/urok-12-mnogomerny-e-massivy-v-java I recommend that you find some tutorial on Java and together programming with him. - Aquinary