In this tutorial, we will learn how to find the size of an array in Java. Finding the size of an array in Java refers to the process of determining the number of elements present in the array. It allows you to ascertain the length or magnitude of the array, enabling you to understand its capacity and efficiently access its elements.
RELATED POST: How to Copy an Array in JAVA?
Find the size or length of an array in JAVA using the following ways
- By using Length Field
- By using size() method
- By using count() method
1. By using length field
In Java, the length
field is an intrinsic property of arrays that provides a convenient and efficient way to determine the size or number of elements in an array. It is a final instance variable that represents the fixed length or capacity of the array, which is established at the time of array creation and cannot be modified thereafter.
The length
field is accessible directly from the array object itself, without requiring any method invocation. It is particularly useful when working with traditional arrays, as it provides a straightforward and efficient means of accessing the array’s size
Example:
public class ArraySizeExample {
public static void main(String[] args) {
int[] myArray = {1, 2, 3, 4, 5}; // Example array
int size = myArray.length; // Accessing the length field of the array
System.out.println("Array size: " + size);
}
}
Output:
Array size: 5
2. By using size() method
The size()
method in Java is a convenient function provided by the ArrayList class, allowing you to determine the size of an array-like structure dynamically. By invoking the size()
method on an ArrayList
object, you retrieve the number of elements currently stored in the list. This method abstracts away the implementation details and provides a consistent way to access the size of the collection, regardless of the underlying array resizing mechanisms.
Example:
import java.util.ArrayList;
public class ArraySizeExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
int size = numbers.size();
System.out.println("ArrayList size: " + size);
}
}
Output
ArrayList size: 5
3. By using the count() method
In Java, there is no direct count()
method to find the size of an array. However, you can achieve the same result using the length
property or the size()
method depending on whether you are working with an array or an ArrayList
, respectively.
The count()
method is not a standard method provided by Java for arrays. If you come across any reference to a count()
method in relation to finding the size of an array, it is likely a custom or user-defined method specific to a particular codebase or library. Without further context or definition, it is not possible to provide a unique definition for a non-standard count()
method.
Example:
import java.util.stream.IntStream;
public class ArraySizeExample {
public static void main(String[] args) {
int[] myArray = {1, 2, 3, 4, 5};
int size = count(myArray);
System.out.println("Array size: " + size);
}
public static int count(int[] array) {
return (int) IntStream.of(array).count();
}
}
Output
Array size: 5