Storing data is a crucial part of a programmer’s job. There always remains a need to store data for the future. There are different methods and ways of storing data according to the type and need. Let us suppose we need to store some values while coding, if we store each and every value in a variable it will create ambiguity in the code. Instead of storing each value in a variable separately, we can use the arrays to store the values. If a large number of values are stored in the array, a need to calculate the size of the array may arise.
In this article, the length or the size of the array will be calculated using different methods of Java.
How to Determine/Calculate the Length or Size of an Array in Java?
In Java, arrays store a fixed number of values of the same type. The values are added to the array before using it. The array in Java is created using two main parameters which are the data type and the size. Therefore, the size of an array is an important aspect of Java. There are multiple methods to calculate the length or the size of the array depicted below:
- length() method
- size() method
- Collection.size() method
- Stream API method
- length() to calculate the character length.
Below is the implementation of methods to fetch the length of the array in Java.
Method 1: Length() Method to find the Array Size in Java
The length() method follows the “array.length()” syntax for the calculation of the array size. The code below uses the length() method to get the array size in Java.
//Declare class for length method.
public class lengthmethod {
//The main class of Java
public static void main(String[] args)
{
//Declare an array with strings
String[] s = { "hello","to","Java","Programming" };
//Print the results using the length() method of Java
System.out.println("PRINT ARRAY USING LENGTH METHOD");
System.out.println("The size of "+ "the array is " + s.length);
}
}
In the code below:
- A class is declared as “lengthmethod” to fetch the length of the array.
- In the main class, an array is declared which consists of strings.
- The length() method prints the array size using the println() method.
Output
The output below shows that the array size is printed as 4 since four elements are passed in the array.
Method 2: size() Method to find the Array Size in Java
size() is a built-in method of the ArrayList class that retrieves the number of list elements. In the code below, the size() method of Java is implemented to get the size of the ArrayList.
import java.util.*;
//The class is declared to implement the size method
public class sizemethod {
public static void main(String[] args)
{
//An array list of integers.
ArrayList<Integer> list= new ArrayList<Integer>();
//Add Integers to the array list.
list.add(11);
list.add(22);
list.add(99);
System.out.println("Array Size Using size() Method");
System.out.println("The Array is: " + list);
int size = list.size();
System.out.println("The Size of array = " + size);
}
}
In the above code:
- First, the required packages are imported.
- A class declared as “sizemethod” determines the array size.
- The Integer values are passed in the ArrayList.
- The add() method inserts a new element to the ArrayList.
- After adding the elements, the size() method is used to determine the size of the array.
- The println() method prints the array size.
Output
The output below shows that there are 3 elements present in the array.
Method 3: Find the Array Size Using the collection.size() Method of Java
The collection.size() method is used in the code below to fetch the array size. The collection method basically returns the values present in the collection. This framework is implemented using different classes like LinkedList, ArrayList, and HashSet.
//Package to import the required package.
import java.util.Collection;
import java.util.HashSet;
//Declare the class
public class collectionmethod {
// main function
public static void main(String[] argv)
{
//Implementation of collection framework using HashSet
Collection<Integer> c = new HashSet<>();
//Insert the items to the list
c.add(11);
c.add(12);
c.add(13);
c.add(14);
c.add(15);
System.out.println("Size of Array using collection.size() Method");
//Print the array
System.out.println("Array: " + c);
int i = c.size();
System.out.println("Size of array = " + i);
}
}
In the above Java code:
- The collection and HashSet packages are imported.
- A class named “collectionmethod” is declared to find the array size.
- The collection framework is implemented using the HashSet class.
Output
The output below depicts that the array list is printed and the size of the array is returned as 5.
Method 4: Find the Array Size Using Stream API
The Stream API was introduced in Java 8. This stream class contains the count() method that allows it to return the size of the array in Java. The below code describes in detail the implementation of Stream API using the stream class of Java.
//Import the required package of Java
import java.util.*;
// The class for the stream API method
public class stream {
public static void main(String[] argv)
{
System.out.println("SIZE USING Stream API METHOD OF JAVA");
int[] intarray = { 11, 12, 13, 14, 15,};
// print integer array
System.out.println("The Integer Array is : "
+ Arrays.toString(intarray));
long i = Arrays.stream(intarray).count();
System.out.println("Size of integer array = " + i);
//Declare a string array
String[] stringarr= { "Hello", "To", "Java", "Programming" };
// print string array
System.out.println("String Array: "
+ Arrays.toString(stringarr));
//The stream method gets the sequential array
long s = Arrays.stream(stringarr).count();
System.out.println("Size of string array = " + s);
}
}
In the above code:
- The java.util package is imported.
- Then a class is declared as the stream for the stream API method.
- The integers are declared in the intarray.
- The Arrays.toString method returns the contents of the specified array in Java.
- The Arrays.stream method returns the specified stream of the data passed in the array.
- The count() method in Java counts the array size.
- The output is printed using the println() method of Java.
Output
The output below depicts the elements in the array, the size of the integer array as 5, and the string array along with its size as 4.
Method 5: Calculate the Character Length of the Array
The length() method returns the number of characters present in the string that is declared as an array in Java. The code below describes the implementation of the length() method to determine the character array length of Java.
//Import the required package
import java.util.*;
//Declare the class for the character array
public class charactersize {
public static void main(String[] argv)
{
//Declare a string
String chararray = "JavaProgramming";
System.out.println("The Character Array is : " + chararray);
//Use the length() method to get the number of characters in the array
int c = chararray.length();
//Print the result
System.out.println("Size of array = " + c);
}
}
In the above block of code:
- The java.util package is imported as per the requirement.
- The charactersize class is declared to calculate the number of characters declared.
- The String “JavaProgramming” is declared.
- The length() method calculates the number of characters declared in the character array.
- The size of the array is declared using the println() method.
Output
The below output prints the characters that are 15 in the string “JavaProgramming” using the length() method.

Conclusion
To determine the length or size of an array in Java, several functions, such as length(), size(), Collection.size(), length(), and the Stream API can be used. All these methods have the same end goal, i.e., to calculate the array size, however, each method follows a different methodology. In this article, we have implemented certain methods to calculate the length of the Java array.