Initialiser un réseau multidimensionnel en Java
Quelle est la bonne façon de déclarer un tableau multidimensionnel et lui affecter des valeurs?
C'est ce que j'ai:
int x = 5;
int y = 5;
String[][] myStringArray = new String [x][y];
myStringArray[0][x] = "a string";
myStringArray[0][y] = "another string";
7 réponses
essayer de remplacer les lignes appropriées par:
myStringArray[0][x-1] = "a string";
myStringArray[0][y-1] = "another string";
 votre code est incorrect parce que les sous-tableaux ont une longueur de y , et l'indexation commence à 0. Ainsi, le réglage à  myStringArray[0][y] ou myStringArray[0][x] échouera parce que les indices x et y  sont hors limites.  
   String[][] myStringArray = new String [x][y];  est la bonne façon d'initialiser un tableau rectangulaire multidimensionnel. Si vous voulez qu'il soit déchiqueté (chaque sous-tableau potentiellement a une longueur différente) alors vous pouvez utiliser le code similaire à    cette réponse    . Notez cependant que L'affirmation de John que vous devez créer les sous-tableaux manuellement est incorrecte dans le cas où vous voulez un tableau multidimensionnel parfaitement rectangulaire.  
Java n'a pas de "vrai" tableaux multidimensionnels.
 par exemple, arr[i][j][k] est équivalent à ((arr[i])[j])[k] . En d'autres termes,  arr  est simplement    un tableau, de tableaux, de tableaux    .  
donc, si vous savez comment les tableaux fonctionnent, vous savez comment les tableaux multidimensionnels fonctionnent!
déclaration:
int[][][] threeDimArr = new int[4][5][6];
ou, avec initialisation:
int[][][] threeDimArr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
accès:
int x = threeDimArr[1][0][1];
ou
int[][] row = threeDimArr[1];
représentation de Chaîne:
Arrays.deepToString(threeDimArr);
rendements
"[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]"
vous pouvez également utiliser la construction suivante:
String[][] myStringArray = new String [][] { { "X0", "Y0"},
                                             { "X1", "Y1"},
                                             { "X2", "Y2"},
                                             { "X3", "Y3"},
                                             { "X4", "Y4"} };
vous pouvez déclarer des tableaux multidimensionnels comme:
// 4 x 5 String arrays, all Strings are null
// [0] -> [null,null,null,null,null]
// [1] -> [null,null,null,null,null]
// [2] -> [null,null,null,null,null]
// [3] -> [null,null,null,null,null]
String[][] sa1 = new String[4][5];
for(int i = 0; i < sa1.length; i++) {           // sa1.length == 4
    for (int j = 0; j < sa1[i].length; j++) {     //sa1[i].length == 5
        sa1[i][j] = "new String value";
    }
}
// 5 x 0  All String arrays are null
// [null]
// [null]
// [null]
// [null]
// [null]
String[][] sa2 = new String[5][];
for(int i = 0; i < sa2.length; i++) {
    String[] anon = new String[ /* your number here */];
    // or String[] anon = new String[]{"I'm", "a", "new", "array"};
    sa2[i] = anon;
}
// [0] -> ["I'm","in","the", "0th", "array"]
// [1] -> ["I'm", "in", "another"]
String[][] sa3 = new String[][]{ {"I'm","in","the", "0th", "array"},{"I'm", "in", "another"}};
Tableau Multidimensionnel en Java
Retourner un tableau multidimensionnel
Java n'est pas vraiment soutien de tableaux multidimensionnels. En Java, un tableau à deux dimensions est simplement un tableau de tableaux, un tableau tridimensionnel est un tableau de tableaux de tableaux, de une à quatre dimensions de la matrice est un tableau de tableaux de tableaux de tableaux, et ainsi de suite...
nous pouvons définir un tableau bidimensionnel comme:
-    int[ ] num[ ] = {{1,2}, {1,2}, {1,2}, {1,2}}
-    int[ ][ ] num = new int[4][2]num[0][0] = 1; num[0][1] = 2; num[1][0] = 1; num[1][1] = 2; num[2][0] = 1; num[2][1] = 2; num[3][0] = 1; num[3][1] = 2;si vous n'attribuez pas, disons num[2][1], il n'est pas initialisé et alors il est automatiquement attribué 0, c'est-à-dire automatiquementnum[2][1] = 0;
-   ci-dessous, num1.lengthvous donne des lignes.
-   alors que  num1[0].lengthvous donne le nombre d'éléments liés à lanum1[0]. Icinum1[0]a des tableaux apparentésnum1[0][0]etnum[0][1]seulement.
-   nous avons utilisé ici une boucle forqui nous aide à calculernum1[i].length. Iciiest incrémenté à travers une boucle.class array { static int[][] add(int[][] num1,int[][] num2) { int[][] temp = new int[num1.length][num1[0].length]; for(int i = 0; i<temp.length; i++) { for(int j = 0; j<temp[i].length; j++) { temp[i][j] = num1[i][j]+num2[i][j]; } } return temp; } public static void main(String args[]) { /* We can define a two-dimensional array as 1. int[] num[] = {{1,2},{1,2},{1,2},{1,2}} 2. int[][] num = new int[4][2] num[0][0] = 1; num[0][1] = 2; num[1][0] = 1; num[1][1] = 2; num[2][0] = 1; num[2][1] = 2; num[3][0] = 1; num[3][1] = 2; If you don't allocate let's say num[2][1] is not initialized, and then it is automatically allocated 0, that is, automatically num[2][1] = 0; 3. Below num1.length gives you rows 4. While num1[0].length gives you number of elements related to num1[0]. Here num1[0] has related arrays num1[0][0] and num[0][1] only. 5. Here we used a 'for' loop which helps us to calculate num1[i].length, and here i is incremented through a loop. */ int num1[][] = {{1,2},{1,2},{1,2},{1,2}}; int num2[][] = {{1,2},{1,2},{1,2},{1,2}}; int num3[][] = add(num1,num2); for(int i = 0; i<num1.length; i++) { for(int j = 0; j<num1[j].length; j++) System.out.println("num3[" + i + "][" + j + "]=" + num3[i][j]); } } }
j'ajouterai que si vous voulez lire les dimensions, vous pouvez le faire:
int[][][] a = new int[4][3][2];
System.out.println(a.length);  // 4
System.out.println(a[0].length); // 3
System.out.println(a[0][0].length); //2
 vous pouvez aussi avoir  tableaux dentelés  , où différentes rangées ont différentes longueurs, donc a[0].length != a[1].length .  
Vous pouvez regarder ceci pour commencer:
    int [][][] i = {                //third dimension curly brace
                     {               // second dimension curly brace
                        {            //first dimension curly brace
                           1,1,1    //elements
                        },           
                    {3,3,3},    
                    {2,2,2}     
                      },
                      {
                         {
                          1,1,1
                         },
                         {3,3,3},
                         {2,2,2}
                       }
                    };