logo

Come trovare la lunghezza dell'array in Java

In Giava, la lunghezza dell'array è il numero di elementi che un array può contenere. Non esiste un metodo predefinito per ottenere il file lunghezza di un array . Possiamo trovare il lunghezza dell'array in Java utilizzando l'attributo array lunghezza . Usiamo questo attributo con il nome dell'array. In questa sezione impareremo come trovare la lunghezza o la dimensione di un matrice in Java .

Attributo lunghezza array

Giava fornisce un attributo lunghezza che determina il lunghezza di un array . Ogni array ha un file integrato lunghezza proprietà il cui valore è la dimensione dell'array. La dimensione implica il numero totale di elementi che un array può contenere. La proprietà length può essere richiamata utilizzando il comando operatore punto (.). seguito dal nome dell'array. Possiamo trovare la lunghezza di int[], double[], String[], ecc. Ad esempio:

 int[] arr=new int[5]; int arrayLength=arr.length 

Nello snippet di codice sopra, arr è un array di tipo int che può contenere 5 elementi. IL arrayLength è una variabile che memorizza la lunghezza di un array. Per trovare la lunghezza dell'array, abbiamo utilizzato il nome dell'array (arr) seguito rispettivamente dall'operatore punto e dall'attributo length. Determina la dimensione dell'array.

Come trovare la lunghezza dell'array in Java

Tieni presente che la lunghezza determina il numero massimo di elementi che l'array può contenere o la capacità dell'array. Non conta gli elementi inseriti nell'array. Cioè, length restituisce la dimensione totale dell'array. Per gli array i cui elementi sono inizializzati al momento della creazione, la lunghezza e la dimensione sono le stesse.

Se parliamo della dimensione logica, dell'indice dell'array, quindi semplicemente int arrayLength=arr.lunghezza-1 , perché l'indice dell'array inizia da 0. Pertanto, l'indice logico o dell'array sarà sempre inferiore di 1 alla dimensione effettiva.

Come trovare la lunghezza dell'array in Java

Troviamo la lunghezza dell'array attraverso un esempio.

ArrayLengthEsempio1.java

 public class ArrayLengthExample1 { public static void main(String[] args) { //defining an array of type int named num //the square bracket contain the length of an array int[] num = new int[10]; //length is an Array attribute that determines the array length int arrayLength=num.length; //prints array length System.out.println('The length of the array is: '+ arrayLength); } } 

Produzione:

 The length of the array is: 10 

ArrayLengthEsempio2.java

 public class ArrayLengthExample2 { public static void main(String[] args) { //initializing an array of type String named country String[] country = { 'India', 'Australia', 'Japan', 'USA', 'UAE', 'Canada', 'Brazil'}; //length is an Array attribute that determines the array length int arrayLength=country.length; //prints array length System.out.println('The size of the array is: ' + arrayLength); } } 

Produzione:

 The size of the array is: 7 

ArrayLengthEsempio3.java

 public class ArrayLengthExample3 { private static void LengthOfArray(String[] array) { //checks array is empty or not if (array == null) { //if the array is empty prints the following statement System.out.println('The array is empty, can't be determined length.'); } else { //length attribute of the Array class determines the length of an array int arrayLength = array.length; //prints the array length System.out.println('The length of the array is: '+arrayLength); } } public static void main(String[] args) { String[] fruits = { 'Guava', 'Banana', 'Apple', 'Papaya', 'Melon', 'Strawberry'}; String[] alphabets = { 'm', 'p', 'k', 'l', 't' }; String[] numbers = { '12', '25', '63', '84', '90', '11', '54'}; //passing null value to the function LengthOfArray(null); //passing fruits array to the function LengthOfArray(fruits); //passing alphabets array to the function LengthOfArray(alphabets); //passing numbers array to the function LengthOfArray(numbers); } } 

Produzione:

 The array is empty, can't be determined length. The length of the array is: 6 The length of the array is: 5 The length of the array is: 7