How to Find Length or Size of an Array in Java?
Last Updated :
18 Apr, 2025
Finding the length of an array is a very common and basic task in Java programming. Knowing the size of an array is essential so that we can perform certain operations. In this article, we will discuss multiple ways to find the length or size of an array in Java.
In this article, we will learn:
- How to find the length of an array using the length property
- Difference between length, length() and size() methods
- Various approaches to find the array length, including loops and Stream API
Different Ways to Find the Length or Size of an Array
1. Using the length Property (Best and Easiest Way)
The length property is the most common way to find the size of an array in Java. It gives the exact number of elements present in the array.
Example: This example demonstrates how to find the length (size) of an array using the length property.
Java
// Finding the length of an Array
public class Geeks {
public static void main(String[] args)
{
// Here arr is the
// array name of int type
int[] arr = new int[4];
System.out.println("The size of the Array is " + arr.length);
}
}
OutputThe size of the Array is 4
Time complexity: O(1) constant time
Note:
- length is a property not a method.
- length property works for all array types (int[] , String[], double[]) etc.
2. Finding Array Length Using a For Loop (Manual Counting)
When iterating over an array, we can also determine its size manually.
Example: This example, demonstrates how to use for each loop to calculate the length of an array.
Java
// Java program to demonstrate for loop
// to calculate length of all type of Arrays
import java.util.*;
public class Geeks {
public static void main(String[] arg) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
int c = 0;
for (int i : arr)
c++;
System.out.println("The Size of the array is " + c);
}
}
OutputThe Size of the array is 7
Time Complexity: O(n) slower than length
Note: This approach is not recommended in real world code.
3. Finding Array Length Using Java 8 Stream API
Stream API was introduced in Java 8 and because of it we can perform various operations on arrays using functional programming. The Stream class provide count() method which is used to count the number of elements in an array.
Example: This example, demonstrates using Arrays.stream() with count() to calculate the length of an array.
Java
// Java program to demonstrate Stream.count()
// method to calculate length of an array
import java.util.*;
public class Geeks {
public static void main(String[] argv)
{
// Creating Array and Populating them
int[] arr = { 1, 2, 3, 4, 5 };
// calculating the length of the arrays
long e = Arrays.stream(arr).count();
// print the length of the arrays
System.out.println("The size of the array is " + e);
}
}
OutputThe size of the array is 5
Time Complexity: O(n)
Note: Avoid Arrays.stream(arr).count() for arrays because it forces unnecessary iteration.
length vs length() vs size()
The difference between length, length() and size() is listed below:
Feature | Used For | Example |
---|
length | It is used for arrays to calculate the number of elements | arr.length |
---|
length() | It is used for strings to calculate the number of characters in the string | s.length() |
---|
size() | It is used for collections like ArrayList or List to calculate the number of elements | list.size() |
---|
Similar Reads
Java Program to Find the Length/Size of an ArrayList Given an ArrayList in Java, the task is to write a Java program to find the length or size of the ArrayList. Examples: Input: ArrayList: [1, 2, 3, 4, 5] Output: 5 Input: ArrayList: [geeks, for, geeks] Output: 3 ArrayList - An ArrayList is a part of the collection framework and is present in java.uti
2 min read
How to Get the Size of an Array in JavaScript To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntaxconst a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that
2 min read
Difference between length of Array and size of ArrayList in Java Array and ArrayList are two different Entities in Java. In this article we will learn the difference between length of Array and size of ArrayList in Java.Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initializa
2 min read
How to Calculate Size of Object in Java? In Java, an object is an instance of a class that encapsulates data and behavior. Calculating the size of an object can be essential for memory management and optimization. This process involves understanding the memory layout of the object, including its fields and the overhead introduced by the JV
2 min read
How to Declare an Array in Java? In Java programming, arrays are one of the most essential data structures used to store multiple values of the same type in a single variable. Understanding how to declare an array in Java is very important. In this article, we will cover everything about array declaration, including the syntax, dif
3 min read
ArrayDeque size() Method in Java The Java.util.ArrayDeque.size() method in Java is used to get the size of the Deque or the number of elements present in the Deque. Syntax: Array_Deque.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Deque.
2 min read
Batch Script - Length of an Array The length of the array is the number of elements in the array. The index of the array starts from "0" to "N-1" where N is a number of elements. For example arr[0]=1 arr[1]=2 arr[2]=3 Here we can see that the index starts from 0 and ends with 2 . So, we know that the index of the element goes from 0
2 min read
Array setLong() method in Java The java.lang.reflect.Array.setLong() is an inbuilt method in Java and is used to set a specified long value to a specified index of a given object array. Syntax: Array.setLong(Object []array, int index, long value) Parameter: array : This is an array of type Object which is to be updated. index : T
3 min read
ArrayList size() Method in Java with Examples In Java, the size() method is used to get the number of elements in an ArrayList.Example 1: Here, we will use the size() method to get the number of elements in an ArrayList of integers.Java// Java program to demonstrate the use of // size() method for Integer ArrayList import java.util.ArrayList; p
2 min read