Java check is array is empty

How to Check if an Array is Empty or Not in Java

In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to check if an array is empty or not. An array is said to be an empty array, if the array has zero element or no element in it.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Suppose the original array is .

After checking if it is empty or not the result will be −

Instance-2

Suppose the original array is <>.

After checking if it is empty or not the result will be −

Instance-3

Suppose the original array is .

After checking if it is empty or not the result will be −

Algorithm

  • Step 1 − Declare and initialize an integer array.
  • Step 2 − Get the length of the array.
  • Step 3 − If length is equal to 0 then array is empty otherwise not.
  • Step 4 − Print the desired result.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Array and length() Method.
  • By Using User Defined Method and length() Method.
  • By Using null Check.

Let’s see the program along with its output one by one.

Approach-1: By Using Static Array and length() Method

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm check if an array is empty or not.

public class Main < //main method public static void main(String[] args) < //Declare and initialize the array elements int arr[] = ; //check if array is empty or not if(arr.length == 0) < //if length is zero then array is empty. System.out.println("Array is empty."); >else < //otherwise array is not empty. System.out.println("Array is not empty."); >> >

Output

Approach-2: By Using User Defined Method and length() Method

Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm check if an array is empty or not.

public class Main< //main method public static void main(String[] args) < //Declare and initialize the array elements int arr[] = <>; //callin user defined method func(arr); > //user defined method public static void func(int arr[]) < //check if array is empty or not if(arr.length == 0) < //if length is zero then array is empty. System.out.println("Array is empty."); >else < //otherwise the array is not empty. System.out.println("Array is not empty."); >> >

Output

Approach-3: By Using null Check

Example

Here array is declared as null means no element. Take an if condition and with the help of equal to operator check array is null or not.

Читайте также:  Светофор

Output

In this article, we explored how to check if an array is empty or not in Java.

Источник

Java – Check if Array is Empty

To check if an array is null, use equal to operator and check if array is equal to the value null.

In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null.

Java Program

public class ArrayExample < public static void main(String[] args) < int arr[] = null; if(arr == null) < System.out.println("The array is empty."); >else < System.out.println("The array is not empty."); >> >

Example 2 – Check if Array is Empty using Length Property

To check if an array has no elements, get length property of the array and check if the length is zero.

In the following example, we will initialize an integer array with empty array. And then use equal to comparison operator in an If Else statement to check if array length is zero.

Java Program

public class ArrayExample < public static void main(String[] args) < int arr[] = <>; if(arr.length == 0) < System.out.println("The array is empty."); >else < System.out.println("The array is not empty."); >> >

Example 3 – Check if Array is Empty using Null Check on Elements

To check if an array has all null elements, use a looping technique and check if the elements are all null.

In the following example, we will initialize an array of user defined objects of type Color with null objects. And then use For loop with an If Else statement to check if all the elements are null.

Java Program

public class ArrayExample < public static void main(String[] args) < Color colors[] = ; boolean empty = true; for(Color color: colors) < if(color != null) < empty = false; break; >> if(empty) < System.out.println("The array is empty."); >else < System.out.println("The array is not empty."); >> > class Color

Example 4 – Check if Array is Empty

In this example, we will combine all the above scenarios and define a function that checks if the array is empty.

Java Program

public class ArrayExample < public static boolean isArrayEmpty(String arr[]) < if(arr == null) < return true; >else if(arr.length == 0) < return true; >else < for(String str: arr) < if(str != null) < return false; >> return true; > > public static void main(String[] args) < String arr[] = null; if(isArrayEmpty(arr)) < System.out.println("arr is empty."); >else < System.out.println("arr is not empty."); >String arr1[] = <>; if(isArrayEmpty(arr1)) < System.out.println("arr1 is empty."); >else < System.out.println("arr1 is not empty."); >String arr2[] = ; if(isArrayEmpty(arr2)) < System.out.println("arr2 is empty."); >else < System.out.println("arr2 is not empty."); >String arr3[] = ; if(isArrayEmpty(arr3)) < System.out.println("arr3 is empty."); >else < System.out.println("arr3 is not empty."); >> >
arr is empty. arr1 is empty. arr2 is empty. arr3 is not empty.

You can change the type of parameter in the isArrayEmpty() function to suit your requirements. We have defined the function to check if a string array is empty.

Conclusion

In this Java Tutorial, we learned how to check if an array is empty in Java. We have defined what an empty array is, and based on that definition, we have given ways to check. Based on your definition of an empty array, you can make the required changes to the conditions that we have to check for an empty array.

Читайте также:  How to center css box

Источник

Check and Declare Empty Array in Java

Java Course - Mastering the Fundamentals

A java empty array is an array that either consist of no elements or all the elements within the array are null . A java empty array can be declared using the new keyword. To check whether an array is empty or not in Java, we can check if its length is 0 or if all the elements in the array are null values. Also, Java provides different libraries to check java empty arrays.

Introduction

Empty array Java is referred to an array in java that has the length 0 or an array that has no elements.

To check if an array is empty in java it should be satisfying one of the following conditions:

  • It should not contain any element, i.e. the size of the array should be 0 .
  • It should be consisting only of null elements.

In further sections, we will learn how to declare a java empty array and how to check if an array is empty in java.

How to Check if An Array is Empty in Java?

Using Null Check

In this section, we will learn how to check for a java empty array using the NULL property. An array can be considered an empty array java if the array displays the value NULL .

Explanation of the Example:

In the above example, we have declared an array dates and assigned the values null . The ‘if condition’ will compare the dates with null . It if equals to null then our program will display «The input array is an empty array» otherwise it will display «The input array is not an empty array» .

Using Length Property

In this section, we will learn how to check for a java empty array using the length property. The length of an array can be used to determine whether it is an empty array or not. If the length of an array is 0 then the given array is an empty array.

Explanation of the example:

In the above example, we have declared an array dates . The dates.length will obtain the size of the array. Iif it equals 0 then our program will display «The input array is an empty array» otherwise it will display «The input array is not an empty array» .

Using Null Check on Elements

In this section, we will learn how to check for a java empty array using the null check on elements property. An array in java is said to be empty if all the elements present in the array are null . Thus we will traverse through the array and if we encounter a non-null element then the array is not empty otherwise it is empty.

Explanation of the example:

In the above example, we have declared an array dates and assigned the value null to each place in the array. The for condition Date it: dates will loop through each element in the dates and compare it with null . If any item does not equal to null then the boolean value isNotEmpty will be assigned true and we will break the loop. If isNotEmpty is false then our program will display «The input array is an empty array» otherwise it will display «The input array is not an empty array» .

Читайте также:  Python unix timestamp to string

Using the Java Library

In this section, we will learn how to check for a java empty array using the Java Library . Java has provided library methods to check if java empty array.

To check for empty array java we need to import the java.util.Arrays class in our Java program. The java.util.Arrays class provides the stream() method which can be used to call the allMatch() method to check the condition of all null values in the array.

Explanation of the example:

In the above example, we have declared an array dates and assigned the value null to each place in the array. The allMatch method will loop through each element in the dates and compare it with null . If any item does not equal null then the boolean value isEmpty will be assigned false and we will break the loop. If isEmpty is false then our program will display «The input array is an empty array» otherwise it will display «The input array is not an empty array» .

Using Apache Commons Library

In this section, we will learn how to check for a java empty array using the Apache commons library .

The isEmpty() function takes an array as a parameter. Then it will check if the parameter passed to it is null or empty. If the array passed as a parameter is null or empty then it would return a true . If the array passed as a parameter is not null or empty then it would return a false .

Note: If you’re using an external library like Apache Commons Lang, you need to make sure that you have added it to your project’s classpath. If the library is not available, you’ll get a «class not found» error.

Explanation of the example:

In the above example, we have declared an array dates and assigned the value null to each place in the array. The ArrayUtils.isEmpty(dates) method will loop through each element in the dates and compare it with null . If any item does not equal null then the boolean value isEmpty will be assigned false and we will break the loop. If isEmpty is false then our program will display «The input array is an empty array» otherwise it will display «The input array is not an empty array» .

How to Initialize an Empty Array in Java?

Using new Keyword

In this section, we will learn how to initialize empty array java using the new keyword.

To initialize an empty array java we need to use the new keyword followed by the data type of the elements the array would store. The size of this array will be 0 .

Using new Keyword with Predefined Size

In the previous section, we defined a java empty array of size 0 . But what if we want to declare a java array of a certain size?

The new keyword can be used to initialize an array java with a predefined size. To initialize an array java with a predefined size we need to use the new keyword followed by the data type of the array and the size of the array.

Conclusion

  • An array is said to be empty in java if it has size 0.
  • An array is said to be empty in java if it displays a null value.
  • An empty array java can be initialized using the new keyword.

Источник

Оцените статью