In Java, the length of an array can be found using the .length
property. This property returns the number of elements in the array. Here’s an example:
java
int[] numbers = {1, 2, 3, 4, 5};
int length = numbers.length; // Returns 5
System.out.println("Array length: " + length);
Note that length
is a field, not a method, so it does not require parentheses. For multidimensional arrays, the .length
property can be used on each dimension to find the length of individual arrays:
java
int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(matrix.length); // Returns 3 (rows)