Maison  >  Article  >  Java  >  Programme Java pour imprimer les éléments du tableau

Programme Java pour imprimer les éléments du tableau

WBOY
WBOYavant
2023-09-22 13:37:021521parcourir

Programme Java pour imprimer les éléments du tableau

Dans cet article, sélectionnez l'élément du tableau et imprimez l'élément en utilisant sa valeur d'index. Les tableaux sont un moyen courant en Java de stocker des types d'éléments similaires ensemble. Les valeurs individuelles ainsi que tous les éléments d'un tableau peuvent être facilement imprimés. Pour l'impression, pour tous les éléments du tableau, une "boucle for" est généralement utilisée, qui sélectionne un index de 0 à la longueur du tableau.

Voici quelques exemples de tableaux de types entiers et chaînes

Exemple de tableau de type Int

int [] array1 = {11,22,32,42,-52,62,-72,82,-92,210};
int [][] array2 = {{11,222},{23,42},{-25,26},{-27,28},{-29,120}};
int [][][] array3 = {{{1111, -22222},{5533, 433}},{{44533, -635533},{-777733, 84433}},{{90033, 84433},{-999933, 433}}};

Exemple de tableau de type chaîne

String[] strarray = new String[]{"One", "Two", "Three"};
String[][] strarray2 = new String[][]{{"One1", "Two2"}, {"Three3", "Four"}};
String[][][] strarray3={{{"One33", "two33"},{"three33", "433"}},{{"44533", "635533"},{"seven33", "84433"}},{{"seven33", "84433"},{"three33", "433"}}};

Algorithme

  • Étape 1 - Déclarez le type et définissez le tableau.

  • Étape 2 - Spécifiez les éléments en fonction du type de tableau. Ces éléments peuvent également être saisis par l'utilisateur.

  • Étape 3 - Commencez à l'élément d'index 0. Imprimez-le.

  • Étape 4 - Augmentez l'index de 1 et imprimez l'élément suivant.

  • Étape 5 - Passez à l'étape 4 et continuez à faire de même jusqu'à ce que le dernier élément du tableau soit imprimé.

  • Étape 6 - Pour les tableaux 2D, utilisez deux boucles distinctes pour contrôler respectivement l'index de ligne et l'index de colonne.

  • Étape 7 - Pour un tableau à N dimensions, utilisez N boucles pour contrôler N index respectivement.

Plusieurs méthodes

Nous fournissons des exemples de solutions utilisant différents types.

  • En utilisant un tableau de type Int

  • En utilisant un tableau de types de chaînes

Regardons le programme et sa sortie un par un.

Méthode/Exemple Type 1 : Utilisation d'un tableau de types entiers

Pour un tableau unidimensionnel

for (int n=0; n<array1.length; n++){
   System.out.println(array1[n]);
} ;

Pour un tableau bidimensionnel

for (int n = 0; n < 2; n++) {
   for (int m=0; m< 2; m++) {
      System.out.print(array2[n][m] + " ");
   }
   System.out.println();
}

Pour les tableaux tridimensionnels

System.out.println("\nThe 3D Int array is:\n ");
for (int n = 0; n < 3; n++)
for (int m=0; m< 2; m++)
for (int t = 0; t < 2; t++)
   System.out.println("array3[" + n + "][" + m + "][" + t + "] = " + array3[n][m][t]);
};

Exemple

public class newarr_multidim {
   public static void main(String[] args) {
      int [] array1 = {11,22,32,42,-52,62,-72,82,-92,210};
      int [][] array2 = {{11,222},{23,42},{-25,26},{-27,28},{-29,120}};
      int [][][] array3 = {{{1111, -22222},{5533, 433}},{{44533, -635533},{-777733, 84433}},{{90033, 84433},{-999933, 433}}};

      //printing individual elements by index value
      System.out.println(array1[1]+ "\n\n" +array2[0][1] + "\n\n" + array3[1][0][1]);

      //printing all elements
      System.out.println("\nThe elements in the 1D int array are:\n");
      for (int n=0; n<array1.length; n++){
         System.out.println(array1[n]);
      } ;
      System.out.println("\nThe 2D Int array is:\n ");
      for (int n = 0; n < 2; n++) {
         for (int m=0; m< 2; m++) {
            System.out.print(array2[n][m] + " ");
         }
         System.out.println();
      }
      System.out.println("\nThe 3D Int array is:\n ");
      for (int n = 0; n < 3; n++)
      for (int m=0; m< 2; m++)
      for (int t = 0; t < 2; t++)
      System.out.println("array3[" + n + "][" + m + "][" + t + "] = " + array3[n][m][t]);
   };
}

Sortie

22
222
-635533
The elements in the 1D int array are:
11
22
32
42
-52
62
-72
82
-92
210
The 2D Int array is:
11 222
23 42
The 3D Int array is:
array3[0][0][0] = 1111
array3[0][0][1] = -22222
array3[0][1][0] = 5533
array3[0][1][1] = 433
array3[1][0][0] = 44533
array3[1][0][1] = -635533
array3[1][1][0] = -777733
array3[1][1][1] = 84433
array3[2][0][0] = 90033
array3[2][0][1] = 84433
array3[2][1][0] = -999933
array3[2][1][1] = 433

Méthode/exemple de type 2 : utilisation d'un tableau de types de chaînes

Pour un tableau unidimensionnel

for (int n=0; n<strarray.length; n++){
   System.out.println(strarray[n]);
} ;

Pour un tableau bidimensionnel

for (int n = 0; n < 2; n++) {
   for (int m=0; m< 2; m++) {
      System.out.print(strarray2[n][m] + " ");
   }
   System.out.println();
}

Pour les tableaux tridimensionnels

System.out.println("\nThe 3D String array is:\n ");
for (int n = 0; n < 3; n++)
for (int m=0; m< 2; m++)
for (int t = 0; t < 2; t++)
System.out.println("strarray3[" + n + "][" + m + "][" + t + "] = " + strarray3[n][m][t]);
};

Exemple

public class newarr_multidim2 {
   public static void main(String[] args) {
      String[] strarray = new String[]{"One", "Two", "Three"};
      String[][] strarray2 = new String[][]{{"One1", "Two2"}, {"Three3", "Four"}};
      String[][][] strarray3={{{"One33", "two33"},{"three33", "433"}},{{"44533", "635533"},{"seven33", "84433"}},{{"seven33", "84433"},{"three33", "433"}}};

      //printing some elements by index value
      System.out.println(strarray[1]+ "\n\n" +strarray2[0][1] + "\n\n" + strarray3[1][0][1]);

      //printing all elements
      System.out.println("\nThe 1D String array is:\n ");
      for (int n=0; n < strarray.length; n++){
         System.out.println(strarray[n]);
      } ;
      System.out.println("\nThe 2D String array is:\n ");
      for (int n = 0; n < 2; n++) {
         for (int m=0; m< 2; m++) {
            System.out.print(strarray2[n][m] + " ");
         }
         System.out.println();
      }
      System.out.println("\nThe 3D String array is:\n ");
      for (int n = 0; n < 3; n++)
      for (int m=0; m< 2; m++)
      for (int t = 0; t < 2; t++)
      System.out.println("strarray3[" + n + "][" + m + "][" + t + "] = " + strarray3[n][m][t]);
   };
}

Sortie

Two
Two2
635533
The 1D String array is:
One
Two
Three
The 2D String array is:
One1 Two2
Three3 Four
The 3D String array is:
strarray3[0][0][0] = One33
strarray3[0][0][1] = two33
strarray3[0][1][0] = three33
strarray3[0][1][1] = 433
strarray3[1][0][0] = 44533
strarray3[1][0][1] = 635533
strarray3[1][1][0] = seven33
strarray3[1][1][1] = 84433
strarray3[2][0][0] = seven33
strarray3[2][0][1] = 84433
strarray3[2][1][0] = three33
strarray3[2][1][1] = 433

Conclusion

Dans l'article ci-dessus, en prenant les types Int et String comme exemples, le langage Java est utilisé pour imprimer les éléments du tableau. Ces exemples incluent l'impression d'éléments de tableau 1D, l'impression d'éléments de tableau 2D et l'impression d'éléments de tableau 3D. Ces méthodes d'impression d'éléments s'étendent aux tableaux à N dimensions.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer