How to check whether an array is empty or not in Java

In this tutorial, we will learn how can we check whether an array is empty or not in Java.

First and foremost, let’s discuss what an array is.

An array can be defined as a linear data structure or a list of finite number N of homogeneous(same data type) data elements such that

  • The element of the array is referenced or addressed respectively by an index set consisting of consecutive numbers.
  • The elements of the array are stored in successive memory allocation.

There is no prior definition to check whether an array is empty or not but we can assume that if there is no element in the array, if all the elements in the array are null and if the array is null then we can conclude that the array is empty.

Approaches to find whether an array is empty or not in Java

(1) Let’s take assumption 1 and code in Java to check whether it can be correct or not.

CODE:

import java.io.*;
class arrayisEmpty{
    public static void main(String[] args)throws IOException{

        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        int arr[]={};
        if(arr.length==0){
            System.out.println("Array is Empty");
        }
        else{
            System.out.println("Array is not Empty");
        }
        
    }
}

OUTPUT:

array is empty or not in Java

Array is Empty

The array arr[] has no element in it so if we use the property arr. length to check the number of elements in the array it will result in zero so the output for the code is “Array is Empty”.

(2)  Assumption 2 (If all the elements in the array are null) but for this, we need an object array.

CODE:

import java.io.*;
class Array{ //class Array

}
class arrayisEmpty{
    public static void main(String[] args)throws IOException{

        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        Array arr[]={null,null,null,null}; //Array class  type array
        int flag=1;
        for(int i=0;i<arr.length;i++){
            if(arr[i]!=null){
                flag=0;
                break;
            }
        }
        if(flag==1)
        System.out.println("Array is Empty");
        else
        System.out.println("Array is not Empty");

        
    }
}

OUTPUT:

Array is Empty.

The array arr[] has a class type datatype whose elements can be null and we all know null means empty indirectly so if all elements in the array are null then the array is empty.

(3)  Assumption (3) when the array is assigned to null.

CODE:

import java.io.*;
class Array{ //class Array

}
class arrayisEmpty{
    public static void main(String[] args)throws IOException{

        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        int arr[]=null;
        if(arr==null){
            System.out.println("Array is Empty");
        } 
        
    }
}

OUTPUT:

Array is Empty.

The array is assigned to null so if we check if the array arr[] is null or not then the condition which is true will get printed and the rest code after that is a dead code for us.

So these were some of the methods where you can check and print whether an array is empty or not. Hope you like my content.

Happy Coding!!!!

Leave a Reply

Your email address will not be published.