There is no size() method available for arrays. But there is a field available in the length array that can be used to find the length or size of the array.
array.length:length is the final variable that applies to the array. With the help of the length variable, we can get the size of the array.
Example:
int size = arr[].length;
//length can be used
//for int[], double[], String[]
//to know the length of the arrays.
Here’s how to use the length variable to get the length of array[] in Java:
Example 1:
//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);
}
}
The output is as follows:
The size of the array is 4
Example 2:
//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);
}
}
The output is as follows:
The size of the array is 3