Computing the length of a container i.e., array, ArrayList of various data types is assistive in Java to analyze the contained data. It is such that this approach assists in appending the data or deleting it efficiently. In addition to that, this approach is of great aid in allocating and sorting the data effectively.
This tutorial will demonstrate how to compute the Arrays’ length in Java.
How to Find Array Length in Java?
The following approaches can be implemented to compute the array’s length in Java:
- “length” Property.
- “for-Each” Loop.
Approach 1: Compute the Array Length Using the “length” Property
The “length” property is applied to compute an array’s length which will be demonstrated in the following code example:
public class Arrayslength {
public static void main(String[] args){
int[] array1 = new int[3];
String[] array2 = {"Java", "Python", "JavaScript"};
System.out.println("Integer Array Length -> "+ array1.length);
System.out.println("String Array Length -> "+ array2.length);
}}
In this snippet of code:
- Initialize an “int” array and allocated the size as “3”.
- Define another array of “String” types comprising the given values.
- Now, associate the “length” property with both arrays to return their corresponding computed lengths.
Output

Based on this output, the following analyses can be done:
- This property evaluated both types of Arrays appropriately.
- The length of the array in both cases, one having allocated size and the other comprising elements is returned accordingly.
Approach 2: Compute the Array Length Using the “for-Each” Loop
The “for-Each” loop iterates through each of the target container elements. This loop can be applied to iterate along an array and retrieve its length based on the iterated elements.
Syntax
for (type variable : array) {
// code block
}
In this syntax, “type” is the “variable” data type, and “array” corresponds to the target array to be iterated.
Now, let’s overview the below-stated demonstration:
public class Arrayslength {
public static void main(String[] args){
Object[] givenArray = { 1, 'b', "David"};
int x = 0;
System.out.print("Array Values -> ");
for (Object i : givenArray) {
System.out.print(i + " ");
x++;
}
System.out.println("\nArray Length -> " + x);
}}
In this block of code:
- Create an array of “Object” types comprising the provided integer and string values.
- Note: The “Object” type can comprise both the “Integer” and “String” types.
- In the next step, initialize the variable “x” which refers to the array’s length.
- Now, apply the “for-Each” loop to iterate all the array elements, and the variable “x” increments by 1 on each iteration.
- Finally, return the array’s length based on the incremented variable till the array’s end.
Output

Here, it can be implied that the array’s length is returned appropriately based on iteration.
Alternative Approach: Compute the ArrayList Length Using the “size()” Method
The “size()” method returns the total number of objects contained in the collection. This method can be implemented to retrieve the length of an “ArrayList” in this case.
Syntax
public int size()
Return Value
This method retrieves the number of items in this list.
Now, proceed to the practical implementation of the discussed method:
import java.util.ArrayList;
public class Arraylength {
public static void main(String[] args){
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);
System.out.println("ArrayList Size -> " +a.size());
}}
Based on this code, perform the below-stated steps:
- Create an ArrayList of the “Integer” type.
- Now, append the stated integers in it via the “add()” method.
- Lastly, retrieve the length/size of the ArrayList via the “size()” method.
Output

The length of the ArrayLength by the inserted elements is returned.
Conclusion
The “length” property or the “for-Each” loop in Java can be applied to compute the array’s length. The “size()” method can be utilized to retrieve the ArrayList’s length. This article demonstrated to find the array’s length in Java.