In short array length means the limit of elements that array can hold. See in the below diagram we have stored 7 elements in array so length of this array is 7.
As shown in above image array length means last index + 1. So in above image array last index is 6. so here array length is 7.
Java Program to find Length of an Array:
public class ArrayLengthExample {
public static void main(String[] args) {
//defining array of length 10
int arr[] = new int[10];
System.out.println("Length of the array is : "+arr.length);
}
}
Length of the array is : 10
Let's see one more Example to find Array Length:
public class ArrayLengthExample2 {
public static void main(String[] args) {
//initializing an array of type string
String str[] = {"Apple","Mango","banana"};
System.out.println("Size of the array is : "+str.length);
}
}
Size of the array is : 3
No comments:
Post a Comment
If you have any doubts, please discuss here...👇