Maison  >  Article  >  Java  >  Comment déterminer la longueur ou la taille d’un tableau en Java ?

Comment déterminer la longueur ou la taille d’un tableau en Java ?

WBOY
WBOYavant
2023-09-17 22:05:07729parcourir

Comment déterminer la longueur ou la taille d’un tableau en Java ?

En Java, l'un des moyens pratiques de déterminer la longueur ou la taille d'un tableau consiste à utiliser sa propriété de longueur. Il compte le nombre d'éléments stockés dans un tableau et renvoie le nombre. une opération courante mais cruciale car elle est utilisée pour trouver le nombre d'éléments d'un tableau, y ajouter de nouveaux éléments et récupérer les éléments stockés. Cet article vise à expliquer les différentes façons d'obtenir la longueur ou la taille d'un tableau.

.

Programme Java pour déterminer la longueur ou la taille d'un tableau

L'exemple suivant nous aidera à comprendre comment trouver la taille d'un tableau.

La traduction chinoise de

Exemple 1

est :

Exemple 1

L'exemple suivant illustre l'utilisation de la propriété length avec un tableau de type entier.

import java.util.*;
public class Example1 {
   public static void main(String[] args) {
      int[] aray = new int[5]; // declaring array of size 5
      // initializing the array
      aray[0] = 11;
      aray[1] = 21;
      aray[2] = 13;
      aray[3] = 23;
      aray[4] = 30;
      // printing the elements of array
	  System.out.println("Elements of the given array: ");
	  for(int elm : aray) {
	     System.out.print(elm + " "); 
	  }
	  System.out.println();
	  // printing the length of array
      System.out.println("Length of the given array: " + aray.length); 
   }
}   

Sortie

Elements of the given array: 
11 21 13 23 30 
Length of the given array: 5

Dans le code ci-dessus, nous avons déclaré un tableau de taille 5, ce qui signifie qu'il peut stocker jusqu'à 5 éléments. Ensuite, à l’aide d’une boucle for-each, nous avons récupéré tous les éléments et déterminé la taille du tableau donné à l’aide de la propriété length.

La traduction chinoise de

Exemple 2

est :

Exemple 2

Dans cet exemple, nous allons déclarer et initialiser un tableau String. Ensuite, en utilisant une boucle for-each, nous imprimerons son élément. À la fin, avec la propriété help length, nous déterminerons la taille du tableau donné.

import java.util.*;
public class Example2 {
   public static void main(String[] args) {
      // declaration and initialization of the array
      String[] aray = { "Tutorix", "Tutorials", "Point", "Simply", "Easy",
         "Learning"            
         }; 
	  // printing the elements of array
	  System.out.println("Elements of the given array: ");
	  for(String elm : aray) {
	     System.out.print(elm + " "); 
      }
	  System.out.println();
	  // printing the length of array
      System.out.println("Length of the given array: " + aray.length); 
   }
}

Sortie

Elements of the given array: 
Tutorix Tutorials Point Simply Easy Learning 
Length of the given array: 6

Exemple 3

Ceci est un autre exemple où nous trouverons la taille d'un tableau sans utiliser aucune propriété ou méthode intégrée de Java.

Méthode

  • Déclarez et initialisez deux tableaux. L’un est un tableau de chaînes et l’autre est un tableau d’entiers

  • Ensuite, définissez deux variables de comptage de type entier pour stocker le nombre d'éléments des deux tableaux.

  • Maintenant, utilisez la boucle for-each pour itérer et incrémenter la variable du compteur de un à chaque itération.

  • Enfin, imprimez les résultats et quittez.

import java.util.*;
public class Example3 {
   public static void main(String[] args) {
      // declaration and initialization of the arrays
      String[] aray1 = { "Tutorix", "Tutorials", "Point", "Simply", "Easy",
         "Learning"            
         }; 
      int[] aray2 = {58, 66, 74, 55, 62};
      // initial counter variable to store the count of elements
      int countr1 = 0;
      int countr2 = 0;
	  // printing the length of both arrays
	  for(String elm : aray1) {
	     countr1++;
	  }
	  for(int elm : aray2) {
	     countr2++;
	  }
      System.out.println("Length of the String array: " + countr1); 
      System.out.println("Length of the integer array: " + countr2); 
   }
}

Sortie

Length of the String array: 6
Length of the integer array: 5

Conclusion

Dans cet article, nous avons appris à utiliser la propriété length pour déterminer la taille d'un tableau donné via un exemple de programme. De plus, nous avons également discuté d'un moyen de trouver la longueur d'un tableau sans utiliser de méthodes ni de propriétés intégrées.

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