public class ArrayLength {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int len = arr.length;
System.out.println("The number of elements in the array is: " + len);
}
}
The number of elements in the array is: 5
arr
and initialize it with the values {1, 2, 3, 4, 5}
. We then use the length
property of the array to get the number of elements in the array and store it in an integer variable len
. System.out.println()
.