Signup/Sign In

Java Program to find the Number of Elements in an Array

In this tutorial, we will learn how to find the total number of elements present in an array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: The Array Elements are: 9 8 7 0 6 5 4 7 3 4 5 2 1

Output: The total number of elements in the array is 13

Program 1: Calculate the Number of Elements present in the Array

In this method, we will see how to calculate the number of elements present in an array using a for each loop.

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Declare a variable count to store the number of elements in the array.
  5. Initialize it to 0.
  6. Use a for each loop to iterate through all the elements in an array.
  7. Increment the count variable in each iteration.
  8. Print the total number of elements in the array.
  9. Now, print the array elements.
  10. Stop.

The below program demonstrates how to calculate the total number of elements in the array using a for each loop. Firstly, we declare an array and then we use a for each loop to determine the total number of elements present in the array.

/*Java Program to find the number of elements present in an array*/

import java.util.*;  
import java.util.Arrays; 

//Driver Code
public class Main  
{  
   public static void main(String args[])   
   {  
        int a[] = {9,8 ,7 ,0 ,6 ,5 ,4 ,7 ,3 ,4 ,5 ,2 ,1};   //Declare and Initialize an array
        int count=0;           //Declare variable to count the number of elements in an array and initialize it to 0
        
        //Use a for each loop to iterate through all the elements in an array
        //Print the elements present in the array
        System.out.println("The entered elements are: ");
        for(int i:a)
        {
             System.out.print(a[i]+" ");
            count++;    //Increment the count variable
        }
        System.out.println("");
        //Print the total number of elements present
        System.out.println("The total number of elements in an array is "+count);
        
       
      
   }
}


The entered elements are:
4 3 7 9 4 5 6 7 0 6 5 7 8
The total number of elements in an array is 13

Program 2: Calculate the Number of Elements present in the Array

In this method, we will see how to calculate the number of elements present in an array using an in-built function. Java provides an in-built function length() that returns the total length of the array. The total length of the array is nothing but the total number of elements present in the array.

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Declare a variable count to store the number of elements in the array.
  5. Use the in-built function to calculate the length of the array.
  6. Print the length of the array.
  7. Using a for loop traverse through all the elements.
  8. Print all the array elements.
  9. Stop.

Below is the code for the same.

The below program demonstrates how to calculate the total number of elements in the array using a try-catch block. Firstly, we declare and initialize the array and then use the in-built function to determine the total number of elements present in the array.

/*Java Program to find the number of elements present in an Array using in-built functions*/

public class Main
{
    //Driver Code
    public static void main(String[] arr)
    {
        int a[] = {91,28 ,47 ,30 ,56 ,65 ,74 ,87 ,93 ,24 ,15 ,82 };   //Declare and Initialize an array
        
        //Declare a variable to store the length of the array
        
        int count=a.length; //Use an in-built function to calculate the length of an array
        
        System.out.println("The number of elements in the array are : "+count); //Print the length of the array
        
        //Print the array elements
        System.out.println("The Array Elements are ");
        
        for(int j=0;j<count;j++)
        {
            System.out.print(a[j]+" ");
        }
        System.out.println("");
    }
}


The number of elements in the array are : 12
The Array Elements are
91 28 47 30 56 65 74 87 93 24 15 82



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.