Monday, January 13, 2025
HomeProgrammingHow to Find Array Length in Java

How to Find Array Length in Java

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)
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x