Table of contents
1.
Introduction
2.
Method 1: Naive Approach to Find Java Array Length
2.1.
Java
3.
Method 2: Using length() Method to find Java Array Size
3.1.
Java
4.
Method 3: Using size() to Find Java array size
4.1.
Java
5.
Method 4: Using Stream API to check Java Array Length
5.1.
Java
6.
Method 5: Using length() method to Check Java Array length
6.1.
Java
7.
Method 6: Using the Collection size() method to find the Java Array size
7.1.
Java
8.
Method 7: Converting Strings in the List to find the size
8.1.
Java
9.
Frequently Asked Questions
9.1.
Can I find the length of a multidimensional array using the same methods?
9.2.
Is there any difference in performance between using the length attribute and the length() method?
9.3.
Can I use the size() method directly on an array without converting it to a list?
10.
Conclusion
Last Updated: Jul 27, 2024
Easy

How to Find Length of Array in Java?

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Arrays are a fundamental part of programming in Java. They allow you to store & access collections of data efficiently. One common task when working with arrays is finding their length or size. 

How to Find Length of Array in Java

In this article, we'll discuss different methods to determine the length of an array in Java, with proper examples & explanations for each approach.

Method 1: Naive Approach to Find Java Array Length

In Java, every array has a built-in length attribute that stores the size of the array. You can access this attribute directly to find the length of an array. 

For example:

  • Java

Java

public class ArrayLengthExample {

   public static void main(String[] args) {

       int[] numbers = {1, 2, 3, 4, 5};

       int length = numbers.length;

       System.out.println("The length of the array is: " + length);

   }

}
You can also try this code with Online Java Compiler
Run Code


In this code, we declare an integer array called `numbers` & initialize it with five elements. To find the length of the array, we simply access the `length` attribute using `numbers.length` & store it in the `length` variable. Finally, we print the length of the array.

Output:

The length of the array is: 5


This naive approach is straightforward & works for arrays of any data type, such as `int`, `String`, `double`, etc. It provides a quick way to retrieve the length of an array without any additional methods or computations.

Method 2: Using length() Method to find Java Array Size

In addition to the naive approach, Java provides the `length()` method to determine the size of an array. This method is part of the `java.lang.reflect.Array` class & can be used with arrays of any type. 

For example:

  • Java

Java

import java.lang.reflect.Array;

public class ArraySizeExample {

   public static void main(String[] args) {

       String[] fruits = {"Apple", "Banana", "Orange", "Mango"};

       int size = Array.getLength(fruits);

       System.out.println("The size of the array is: " + size);

   }

}
You can also try this code with Online Java Compiler
Run Code


In this code, we have an array of strings called `fruits` containing four elements. To find the size of the array, we use the `Array.getLength()` method, passing the array as an argument. The method returns the size of the array, which we store in the `size` variable & then print.

Output:

The size of the array is: 4


The `length()` method provides a more dynamic way to find the size of an array, as it can be used with arrays of any type. It is particularly useful when you have a reference to an array object & need to determine its size without knowing the specific array variable.

Method 3: Using size() to Find Java array size

Another way to find the size of an array in Java is by using the `size()` method from the `java.util.ArrayList` class. However, this method is only applicable when you have an `ArrayList` containing the array elements. Here's an example:

  • Java

Java

import java.util.ArrayList;

import java.util.Arrays;

public class ArraySizeExample {

   public static void main(String[] args) {

       Integer[] numbers = {1, 2, 3, 4, 5};

       ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(numbers));

       int size = arrayList.size();

       System.out.println("The size of the array is: " + size);

   }

}
You can also try this code with Online Java Compiler
Run Code


In this code, we have an array of integers called `numbers`. To find its size using the `size()` method, we first need to convert the array to an `ArrayList`. We achieve this by using the `Arrays.asList()` method, which takes the array as an argument & returns a fixed-size list backed by the array. We then create a new `ArrayList` called `arrayList` using the constructor that accepts the list.

Once we have the `ArrayList`, we can simply call the `size()` method on it to retrieve the size of the underlying array. The size is stored in the `size` variable & then printed.

Output:

The size of the array is: 5


Using the `size()` method in combination with an `ArrayList` provides a convenient way to find the size of an array, especially when you need to perform operations specific to lists.

Method 4: Using Stream API to check Java Array Length

Java 8 introduced the Stream API, which provides a functional programming approach to process collections of data, including arrays. We can use the Stream API to find the length of an array by converting it to a stream and counting the elements. 

For example:

  • Java

Java

import java.util.Arrays;

public class ArrayLengthExample {

   public static void main(String[] args) {

       String[] names = {"Rahul", "Rinki", "Harsh", "Sanjana"};

       long length = Arrays.stream(names).count();

       System.out.println("The length of the array is: " + length);

   }

}
You can also try this code with Online Java Compiler
Run Code


In this code, we have an array of strings called `names` containing four elements. To find the length of the array using the Stream API, we first convert the array to a stream using `Arrays.stream()`, passing the array as an argument. This returns a stream of strings.

Next, we use the `count()` method on the stream, which returns the number of elements in the stream as a long value. We store the count in the `length` variable and then print it.

Output:

The length of the array is: 4


Using the Stream API to find the length of an array provides a concise and functional approach. It can be particularly useful when you need to perform additional stream operations on the array, such as filtering or mapping, before finding the length.

Method 5: Using length() method to Check Java Array length

In Java, you can also use the `length()` method from the `java.lang.reflect.Array` class to check the length of an array. This method is similar to the one we discussed in Method 2, but it provides a more general approach.

For example:

  • Java

Java

import java.lang.reflect.Array;

public class ArrayLengthExample {

   public static void main(String[] args) {

       double[] prices = {9.99, 14.5, 7.25, 12.0};

       int length = Array.getLength(prices);

       System.out.println("The length of the array is: " + length);

   }

}
You can also try this code with Online Java Compiler
Run Code


In this code, we have an array of doubles called `prices` containing four elements. To find the length of the array, we use the `Array.getLength()` method from the `java.lang.reflect.Array` class, passing the array as an argument. The method returns the length of the array as an integer value, which we store in the `length` variable and then print.

Output:

The length of the array is: 4


The `length()` method from the `java.lang.reflect.Array` class is a versatile way to check the length of an array, as it works with arrays of any type. It can be particularly useful when you have a reference to an array object and need to determine its length without knowing the specific array variable.

Method 6: Using the Collection size() method to find the Java Array size

Another approach to find the size of an array in Java is by converting it to a collection and then using the `size()` method. 

For example:

  • Java

Java

import java.util.Arrays;

import java.util.List;

public class ArraySizeExample {

   public static void main(String[] args) {

       String[] names = {"Ravi", "Mehak", "Sinki", "Harsh"};

       List<String> nameList = Arrays.asList(names);

       int size = nameList.size();

       System.out.println("The size of the array is: " + size);

   }

}
You can also try this code with Online Java Compiler
Run Code


In this code, we have an array of strings called `names` containing four elements. To find the size of the array using the `size()` method, we first need to convert the array to a collection. We achieve this by using the `Arrays.asList()` method, which takes the array as an argument and returns a fixed-size list backed by the array. We assign this list to a variable called `nameList`.

Once we have the list, we can simply call the `size()` method on it to retrieve the size of the underlying array. The size is stored in the `size` variable and then printed.

Output:

The size of the array is: 4


Using the `size()` method in combination with converting the array to a collection provides another way to find the size of an array. This approach can be handy when you need to perform operations specific to collections or when working with APIs that expect collections.

Method 7: Converting Strings in the List to find the size

In Java, you can also find the size of an array by converting the elements to strings and then using the `size()` method. This approach is particularly useful when you have an array of objects and want to find the size based on their string representations. 

For example:

  • Java

Java

import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;

public class ArraySizeExample {

   public static void main(String[] args) {

       Person[] people = {

           new Person("Rahul", 25),

           new Person("Rinki", 30),

           new Person("Harsh", 28)

       };

       List<String> nameList = Arrays.stream(people)

               .map(Person::getName)

               .collect(Collectors.toList());

       int size = nameList.size();

       System.out.println("The size of the array is: " + size);

   }

   static class Person {

       private String name;

       private int age;

       public Person(String name, int age) {

           this.name = name;

           this.age = age;

       }

       public String getName() {

           return name;

       }

   }

}
You can also try this code with Online Java Compiler
Run Code

In this code, we have an array of `Person` objects called `people`. Each `Person` object has a `name` and an `age` attribute. To find the size of the array based on the names, we first use the `Arrays.stream()` method to convert the array to a stream.

Next, we use the `map()` method on the stream to extract the names of each person using the `Person::getName` method reference. This creates a new stream containing only the names.

We then use the `collect()` method with `Collectors.toList()` to convert the stream of names to a list called `nameList`.

Finally, we call the `size()` method on the `nameList` to get the size of the array based on the number of names. The size is stored in the `size` variable and then printed.

Output:

The size of the array is: 3


This approach demonstrates how you can find the size of an array by converting its elements to strings and using the `size()` method on the resulting list. It can be helpful when you want to find the size based on specific attributes of the objects in the array.

Frequently Asked Questions

Can I find the length of a multidimensional array using the same methods?

Yes, you can use the length attribute or Array.getLength() method for multidimensional arrays as well.

Is there any difference in performance between using the length attribute and the length() method?

No, both approaches have the same performance as they directly access the array's length information.

Can I use the size() method directly on an array without converting it to a list?

No, the size() method is not available for arrays. You need to convert the array to a list or use other methods.

Conclusion

In this article, we explained various methods to find the length or size of an array in Java. We covered the naive approach using the length attribute, the length() method from the java.lang.reflect.Array class, the size() method with ArrayList, the Stream API approach, and converting arrays to collections. Each method has its own use case and can be chosen based on the specific requirements of your program.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass