Find the length of Array in Java

311
find the array length in java
Array length in java
Advertisement

 

Find the length of Array in Java. That’s a very basic but very interesting question. whenever you start coding with the array. Like you need to print the array elements, search an element in the array or modify the element in the array. In all cases, you need to know the size of the array, because array size needs to be used to iterate over the array. We are implementing here some ways to find the length of the array, hope you all will find it helpful in your knowledge bank.

 

Method 1: Using the length :

The first way to find the length of the array is very simple. You just need to use the length with your array object.

Program to find the Array Length using the length:

public class ArrayLength
{
  public static void main(String[] args) {
    
    //Initializing an Array
    
    int[] array = {4,3,6,8,9,3,10,67,3,6,8,9,23,45,32,78,97,94,39,25,60,20,24,28,39,59,69,54,55,11,26};
    
    int arrayLength = array.length;
    System.out.println("Array Length is: "+arrayLength);
  }
}

 

Output:

Array Length is: 31

 

Method 2: Using Infinite While Loop:

This is the second method to find the Array length using an infinite while loop. In this method, we are using an infinite while loop that runs from 0 over the array indexes and terminated just after an index above the array with a Runtime Error Message ArrayIndexOutOfBoundException.

The drawback of this method is, it is not very useful while solving the real-world problems because we need an error-free solution and it’s not an error-free solution but it is a way to find Array length.

“This method is only for knowledge purpose don’t try to implement in your real-world problems.”

Program to find Array Length using Infinite While loop with Error:

public class ArrayLength
{
    
    public static void ArrayLength(int[] array){
        
        int index=0;  // Variable to iterate over the Array
      int i=1;      // Dummy Variable to stick the while loop infinite
      int count =0; // Count variable for counting the array length
      
          while(i>0){
              int num = array[index];
              index++;
              System.out.println("Array Length: "+index);
              count++;
          }
      
    }
    
  public static void main(String[] args) {
      
      // A Dummy array to find the Length
      int[] a = {17,23,34,94,75,26,67,29};
      
      ArrayLength(a);
      
      
  }
}

 

Output:

Array Length: 1
Array Length: 2
Array Length: 3
Array Length: 4
Array Length: 5
Array Length: 6
Array Length: 7
Array Length: 8

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
  at GFG.ArrayLength(GFG.java:11)
  at GFG.main(GFG.java:24)

The last Array Length: 8 is the length of the array and after that, it’s throwing an exception message.

 

Method 3: Using try-catch-finally Block with Infinite While Loop:

In this method, we are using the same technique as used in method 2 but the good thing is we are handling the error in this method using try-catch block.

Your next question could be like: What try-catch-finally blocks do?  

try-catch-finally block is used to handle the runtime error in programming languages like java, python etc;

  • try block: try block just try to execute the statements written under the try-block. If any runtime error/exception occurs then control goes from the try block to catch block.
  • catch block: when an error occurs in try block then control goes to catch block, here the error occurred in try block categorized in this block and also print the error message with the specified error/Exception Name like ArrayIndexOutOfBoundException in this case.
  • finally block: finally-block is always executed no matter what happens means if there is an error or not.

Program to find the length of an array using a try-catch-finally block:

public class ArrayLength
{
    
    public static void ArrayLength(int[] array){
        
        int index=0;  // Variable to iterate over the Array
      int i=1;      // Dummy Variable to stick the while loop infinite
      int count =0; // Count variable for counting the array length
      
        try{
          while(i>0){
              int num = array[index];
              index++;
              count++;
              
          }
      }catch(Exception e){
          System.out.println("Array Ends Here.");
          
      }finally{
          System.out.println("Array Lenght: "+count);
          
      }
      
    }
    
  public static void main(String[] args) {
      
      // A Dummy array to find the 
      int[] a = {17,23,34,94,75,26,67,89};
      
      ArrayLength(a);
      
  }
}

 

Output:

Array Ends Here.                                                                                                                                     
Array Lenght: 8

As you see the difference,  In method 2 if a runtime error occurs then it can disturb the flow of the program. The Exception Handling concept described in method 3 to handle the run time error in real-world problems.

Hope you all like this post very helpful for your knowledge. If you want to check out our other post’s related to an array then click here

If you want to practice the pattern programs in Java, then you should follow the below link for the pattern programs: Pattern program in Java