En Java, un tableau est une structure de données qui stocke une collection d'éléments de taille fixe du même type. Pour déterminer la longueur ou la taille d'un tableau en Java, nous pouvons utiliser différentes méthodes.
Méthode 1 : Approche naïve pour trouver la longueur du tableau Java
La méthode naïve est utilisée pour la boucle pour déterminer la taille/longueur des types de tableaux char, entier et chaîne.
Vous trouverez ci-dessous la mise en œuvre de l’approche ci-dessus :
Java
// Java program to demonstrate for loop> // to calculate size/length of all type of arrays> > import> java.util.*;> > public> class> Main {> >public> static> void> main(String[] argv)> >{> > >// Creating Arrays and Populating them> >char>[] char_arr = {>'a'>,>'b'>,>'c'>,>'d'>,>'e'> };> >int>[] int_arr = {>1>,>2>,>3>,>4>,>5>,>6>,>7> };> >String[] str_arr> >= {>'GFG'>,>'GEEKS'>,>'GEEKSFORGEEKS'> };> > >int> ci =>0>, ii =>0>, si =>0>;> > >// print char array> >System.out.print(>'Char Array: [ '>);> >for> (>char> c : char_arr) {> >System.out.print(>'''> + c +>'' '>);> >ci++;> >}> >System.out.println(>']'>);> > >// print integer array> >System.out.print(>'Integer Array: [ '>);> >for> (>int> c : int_arr) {> >System.out.print(c +>' '>);> >ii++;> >}> >System.out.println(>']'>);> > >// print string array> >System.out.print(>'String Array: [ '>);> >for> (String c : str_arr) {> >System.out.print(>'''> + c +>'' '>);> >si++;> >}> >System.out.println(>']
'>);> > >// print the size/length of all arrays> >System.out.println(>'Size of char array = '> + ci);> >System.out.println(>'Size of integer array = '> + ii);> >System.out.println(>'Size of string array = '> + si);> >}> }> > // This code is contributed by Susobhan Akhuli> |
>
>Sortir
Char Array: [ 'a' 'b' 'c' 'd' 'e' ] Integer Array: [ 1 2 3 4 5 6 7 ] String Array: [ 'GFG' 'GEEKS' 'GEEKSFORGEEKS' ] Size of char array = 5 Size of integer array = 7 Size of string array = 3>
La complexité de la méthode ci-dessus
Complexité temporelle : O(N), où N est la taille du tableau.
Espace auxiliaire : O(1)
Méthode 2 : Utilisation de la méthode length() pour trouver la taille du tableau Java
Il y a un longueur champ disponible dans le tableau qui peut être utilisé pour trouver la longueur ou la taille du tableau.
tableau.longueur : length est une variable finale applicable aux tableaux. A l'aide de la variable length, nous pouvons obtenir la taille du tableau.
Exemples:
int size = arr[].length; // length can be used // for int[], double[], String[] // to know the length of the arrays.>
Vous trouverez ci-dessous l'illustration de la façon d'obtenir la longueur d'un tableau[] en Java à l'aide de la variable length :
Exemple 1:
Java
// Java program to illustrate> // how to get the length of the array> > public> class> Test {> >public> static> void> main(String[] args)> >{> > >// Here array is the> >// array name of int type> >int>[] array =>new> int>[>4>];> > >System.out.println(>'The size of '> >+>'the array is '> >+ array.length);> >}> }> |
>
>Sortir
The size of the array is 4>
Exemple 2 :
Java
// Java program to illustrate> // how to get the length of the array> > public> class> Test {> >public> static> void> main(String[] args)> >{> > >// Here str is the array name> >// of String type.> >String[] str = {>'GEEKS'>,>'FOR'>,>'GEEKS'> };> > >System.out.println(>'The size of '> >+>'the array is '> + str.length);> >}> }> |
>
>Sortir
Sree Ramanujan
The size of the array is 3>
La complexité de la méthode ci-dessus
Complexité temporelle : O(1)
Espace auxiliaire : O(1)
Méthode 3 : utilisation de size() pour rechercher la taille du tableau Java
Alternativement, nous pouvons utiliser le taille() méthode du java.util.ArrayList classe, qui renvoie le nombre d’éléments dans la liste.
le Exemple 1:
Java
// Java program to demonstrate> // size() method> // for Integer value> > import> java.util.*;> > public> class> GFG1 {> >public> static> void> main(String[] argv)> >{> > >// Creating object of ArrayList> >ArrayList arrlist> >=>new> ArrayList();> > >// Populating arrlist1> >arrlist.add(>1>);> >arrlist.add(>2>);> >arrlist.add(>3>);> >arrlist.add(>4>);> >arrlist.add(>5>);> > >// print arrlist> >System.out.println(>'Array: '> + arrlist);> > >// getting total size of arrlist> >// using size() method> >int> size = arrlist.size();> > >// print the size of arrlist> >System.out.println(>'Size of array = '> + size);> >}> }> > // This code is contributed by Susobhan Akhuli> |
>
>Sortir
Array: [1, 2, 3, 4, 5] Size of array = 5>
Exemple 2 :
Java
// Java program to demonstrate> // size() method> // for String value> > import> java.util.*;> > public> class> GFG1 {> >public> static> void> main(String[] argv)> >{> > >// Creating object of ArrayList> >ArrayList arrlist =>new> ArrayList();> > >// Populating arrlist1> >arrlist.add(>'GFG'>);> >arrlist.add(>'GEEKS'>);> >arrlist.add(>'GEEKSFORGEEKS'>);> > >// print arrlist> >System.out.println(>'Array: '> + arrlist);> > >// getting total size of arrlist> >// using size() method> >int> size = arrlist.size();> > >// print the size of arrlist> >System.out.println(>'Size of array = '> + size);> >}> }> > // This code is contributed by Susobhan Akhuli> |
>
>Sortir
Array: [GFG, GEEKS, GEEKSFORGEEKS] Size of array = 3>
La complexité de la méthode ci-dessus
Complexité temporelle : O(1)
Espace auxiliaire : O(1)
Méthode 4 : Utilisation de l'API Stream pour vérifier la longueur du tableau Java
Java 8 a introduit le API de flux , qui nous permet d'effectuer des opérations sur des tableaux en utilisant la programmation fonctionnelle. Le compter() méthode du Flux La classe peut être utilisée pour compter le nombre d’éléments dans un tableau.
Vous trouverez ci-dessous la mise en œuvre de l’approche ci-dessus :
Java
// Java program to demonstrate Stream.count()> // method to calculate size/length of> // different arrays> import> java.util.*;> > // Driver Class> public> class> Main {> >// main function> >public> static> void> main(String[] argv)> >{> >// Creating Array and Populating them> >int>[] int_arr = {>1>,>2>,>3>,>4>,>5>,>6>,>7> };> >String[] str_arr> >= {>'GFG'>,>'GEEKS'>,>'GEEKSFORGEEKS'> };> > >// print integer array> >System.out.println(>'Integer Array: '> >+ Arrays.toString(int_arr));> > >// print string array> >System.out.println(>'String Array: '> >+ Arrays.toString(str_arr)> >+>'
'>);> > >// calculating the size/length of the arrays> >long> ii = Arrays.stream(int_arr).count();> >long> si = Arrays.stream(str_arr).count();> > >// print the size/length of the arrays> >System.out.println(>'Size of integer array = '> + ii);> >System.out.println(>'Size of string array = '> + si);> >}> }> |
>
>Sortir
Integer Array: [1, 2, 3, 4, 5, 6, 7] String Array: [GFG, GEEKS, GEEKSFORGEEKS] Size of integer array = 7 Size of string array = 3>
La complexité de la méthode ci-dessus
Complexité temporelle : O(1)
Espace auxiliaire : O(1)
Méthode 5 : Utilisation de la méthode length() pour vérifier la longueur du tableau Java
Le longueur() la méthode est une méthode du java.lang.String classe qui renvoie uniquement le nombre de caractères dans la chaîne, qui est un tableau de caractères. Cette méthode ne prend aucun argument et renvoie un int Type de données.
Vous trouverez ci-dessous la mise en œuvre de la méthode ci-dessus :
Java
// Java program to demonstrate length() method> // to calculate size/length of only char array> import> java.util.*;> > // Driver Class> public> class> Main {> >// main function> >public> static> void> main(String[] argv)> >{> >// Creating Array of character> >// and Populating them> >String char_arr =>'GEEKSFORGEEKS'>;> > >// print char array> >System.out.println(>'Char Array: '> + char_arr);> > >// calculating the size/length of the array> >int> ci = char_arr.length();> > >// print the size/length of the array> >System.out.println(>'Size of integer array = '> + ci);> >}> }> |
>
>Sortir
Char Array: GEEKSFORGEEKS Size of integer array = 13>
N.B. : La variable length est applicable à tous les types de tableau tandis que la méthode length() n'est applicable qu'aux objets chaîne (tableau de caractères).
Méthode 6 : Utilisation de la méthode Collection size() pour trouver la taille du tableau Java
Le collection.size() la méthode est une méthode du java.util.Collection interface, qui est implémentée par de nombreuses classes dans Java Collections Framework. Cette méthode renvoie le nombre d'éléments dans la collection. Le Collection interface est une interface racine dans Java Collection Framework et elle est implémentée par de nombreuses classes telles que ArrayList, LinkedList, HashSet et TreeSet.
Vous trouverez ci-dessous la mise en œuvre de la méthode ci-dessus :
Java
// Java program to demonstrate Collection.size() method> // to calculate size/length of array> import> java.util.Collection;> import> java.util.HashSet;> > // Driver Class> public> class> Main {> >// main function> >public> static> void> main(String[] argv)> >{> >// Creating collection> >Collection collection =>new> HashSet();> > >// Populating them> >collection.add(>1>);> >collection.add(>2>);> >collection.add(>3>);> >collection.add(>4>);> >collection.add(>5>);> >collection.add(>6>);> >collection.add(>7>);> > >// print it> >System.out.println(>'Array: '> + collection);> > >// calculating the size/length of the array> >int> ii = collection.size();> > >// print the size/length of the array> >System.out.println(>'Size of array = '> + ii);> >}> }> |
>
>Sortir
Array: [1, 2, 3, 4, 5, 6, 7] Size of array = 7>
Méthode 7 : Conversion des chaînes dans la liste pour trouver la taille
Le Arrays.asList(myArray).size() La méthode est utilisée pour renvoyer la taille d’un tableau lorsqu’il est converti en liste. La taille du tableau est égale au nombre d’éléments du tableau.
Vous trouverez ci-dessous la mise en œuvre de la méthode ci-dessus :
Java
// Java program to demonstrate Stream.count() method> // to calculate size/length of different arrays> > import> java.util.*;> > // Driver Class> public> class> GFG {> >// main function> >public> static> void> main(String[] argv)> >{> >// Creating String Array> >String[] str_arr> >= {>'GFG'>,>'GEEKS'>,>'GEEKSFORGEEKS'> };> > >// print string array> >System.out.println(>'String Array: '> >+ Arrays.toString(str_arr)> >+>'
'>);> > >// calculating the size/length of the array> >long> si = Arrays.asList(str_arr).size();> > >// print the size/length of the array> >System.out.println(>'Size of string array = '> + si);> >}> }> |
>
>Sortir
String Array: [GFG, GEEKS, GEEKSFORGEEKS] Size of string array = 3>