How to determine length or size of an Array in Java?


In Java, one of the convenient ways to determine the length or size of an array is by using its length property. It counts the number of elements stored in an array and returns the count. Finding the length of an array is one of the common yet crucial operation as it is used to find the number of elements of an array, append new elements into it and retrieve stored items. This article aims to explain the various ways to get the length or size of an array.

Java Program to determine the length or size of an Array

Below examples will help us to understand how we can find the size of an array.

Example 1

The following example illustrates the use of length property with an array of type integer.

import java.util.*;
public class Example1 {
   public static void main(String[] args) {
      int[] aray = new int[5]; // declaring array of size 5
      // initializing the array
      aray[0] = 11;
      aray[1] = 21;
      aray[2] = 13;
      aray[3] = 23;
      aray[4] = 30;
      // printing the elements of array
	  System.out.println("Elements of the given array: ");
	  for(int elm : aray) {
	     System.out.print(elm + " "); 
	  }
	  System.out.println();
	  // printing the length of array
      System.out.println("Length of the given array: " + aray.length); 
   }
}   

Output

Elements of the given array: 
11 21 13 23 30 
Length of the given array: 5

In the above code, we have declared an array of size 5 means it can store a maximum of 5 elements. Then, using a for-each loop, we retrieved all the elements and further with the help length property, we determined the size of given array.

Example 2

In this example, we will declare and initialize a String array. Then, using a for-each loop, we will print its element. In the end, with the help length property, we determine the size of given array.

import java.util.*;
public class Example2 {
   public static void main(String[] args) {
      // declaration and initialization of the array
      String[] aray = { "Tutorix", "Tutorials", "Point", "Simply", "Easy",
         "Learning"            
         }; 
	  // printing the elements of array
	  System.out.println("Elements of the given array: ");
	  for(String elm : aray) {
	     System.out.print(elm + " "); 
      }
	  System.out.println();
	  // printing the length of array
      System.out.println("Length of the given array: " + aray.length); 
   }
}

Output

Elements of the given array: 
Tutorix Tutorials Point Simply Easy Learning 
Length of the given array: 6

Example 3

This is another example where we will find the size of an array without using any in-built property or method of Java.

Approach

  • Declare and initialize two arrays. One of String and the other one of integer.

  • Then, define two counter variables of integer type to store the count of elements of both arrays.

  • Now, use the for-each loop to iterate and increment the counter variable by one with each iteration.

  • Finally, print the result and exit.

import java.util.*;
public class Example3 {
   public static void main(String[] args) {
      // declaration and initialization of the arrays
      String[] aray1 = { "Tutorix", "Tutorials", "Point", "Simply", "Easy",
         "Learning"            
         }; 
      int[] aray2 = {58, 66, 74, 55, 62};
      // initial counter variable to store the count of elements
      int countr1 = 0;
      int countr2 = 0;
	  // printing the length of both arrays
	  for(String elm : aray1) {
	     countr1++;
	  }
	  for(int elm : aray2) {
	     countr2++;
	  }
      System.out.println("Length of the String array: " + countr1); 
      System.out.println("Length of the integer array: " + countr2); 
   }
}

Output

Length of the String array: 6
Length of the integer array: 5

Conclusion

In this article, we have learned the use of length property in determining the size of a given array with the help of example programs. Also, we discussed a way to find the length of an array without using any built-in method and property.

Updated on: 19-Jul-2023

758 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements