V Javi je polje podatkovna struktura, ki shranjuje zbirko elementov iste vrste s fiksno velikostjo. Za določitev dolžine ali velikosti matrike v Javi lahko uporabimo različne metode.
1. način: Naivni pristop za iskanje dolžine polja Java
Naivna metoda se uporablja za zanko za določanje velikosti/dolžine vrste matrik char, integer in string.
Spodaj je izvedba zgornjega pristopa:
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> |
>
>Izhod
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>
Kompleksnost zgornje metode
Časovna zapletenost: O(N), kjer je N velikost niza.
Pomožni prostor: O(1)
2. način: Uporaba metode length() za iskanje velikosti polja Java
Obstaja a dolžina polje, ki je na voljo v matriki in se lahko uporabi za iskanje dolžine ali velikosti matrike.
array.length: dolžina je končna spremenljivka, ki se uporablja za nize. S pomočjo spremenljivke dolžine lahko dobimo velikost matrike.
Primeri:
int size = arr[].length; // length can be used // for int[], double[], String[] // to know the length of the arrays.>
Spodaj je ilustracija, kako pridobiti dolžino matrike [] v Javi z uporabo spremenljivke dolžine:
Primer 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);> > }> }> |
>
>Izhod
The size of the array is 4>
Primer 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);> > }> }> |
>
>Izhod
topologija omrežja
The size of the array is 3>
Kompleksnost zgornje metode
Časovna zapletenost: O(1)
Pomožni prostor: O(1)
3. način: Uporaba size() za iskanje velikosti polja Java
Lahko pa uporabimo tudi velikost () metoda java.util.ArrayList razred, ki vrne število elementov na seznamu.
the Primer 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> |
>
>Izhod
Array: [1, 2, 3, 4, 5] Size of array = 5>
Primer 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> |
>
>Izhod
Array: [GFG, GEEKS, GEEKSFORGEEKS] Size of array = 3>
Kompleksnost zgornje metode
Časovna zapletenost: O(1)
Pomožni prostor: O(1)
4. način: Uporaba API-ja Stream za preverjanje dolžine polja Java
Java 8 je predstavila Stream API , ki nam omogoča izvajanje operacij nad nizi s pomočjo funkcijskega programiranja. The štetje () metoda Tok razred lahko uporabite za štetje števila elementov v matriki.
Spodaj je izvedba zgornjega pristopa:
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);> > }> }> |
>
>Izhod
Integer Array: [1, 2, 3, 4, 5, 6, 7] String Array: [GFG, GEEKS, GEEKSFORGEEKS] Size of integer array = 7 Size of string array = 3>
Kompleksnost zgornje metode
Časovna zapletenost: O(1)
Pomožni prostor: O(1)
5. način: Uporaba metode length() za preverjanje dolžine polja Java
The dolžina() metoda je metoda java.lang.String razred, ki vrne samo število znakov v nizu, ki je niz znakov. Ta metoda ne sprejema nobenih argumentov in vrne int tip podatkov.
Spodaj je izvedba zgornje metode:
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);> > }> }> |
>
>Izhod
Char Array: GEEKSFORGEEKS Size of integer array = 13>
Opomba: Spremenljivka dolžina je uporabna za vse vrste nizov, medtem ko je metoda length() uporabna samo za objekte niz (niz znakov).
6. način: Uporaba metode Collection size() za iskanje velikosti Java Array
The zbirka.velikost() metoda je metoda java.util.Collection vmesnik, ki ga implementira veliko razredov v Java Collections Framework. Ta metoda vrne število elementov v zbirki. The Zbirka vmesnik je korenski vmesnik v zbirki Java Framework in ga izvajajo številni razredi, kot so ArrayList, LinkedList, HashSet in TreeSet.
Spodaj je izvedba zgornje metode:
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);> > }> }> |
>
>Izhod
Array: [1, 2, 3, 4, 5, 6, 7] Size of array = 7>
7. način: Pretvarjanje nizov na seznamu za iskanje velikosti
The Arrays.asList(myArray).size() metoda se uporablja za vrnitev velikosti matrike, ko je pretvorjena v seznam. Velikost matrike je enaka številu elementov v matriki.
Spodaj je izvedba zgornje metode:
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);> > }> }> |
>
>Izhod
String Array: [GFG, GEEKS, GEEKSFORGEEKS] Size of string array = 3>