Empty array in Java
An empty array in Java is one that either has no elements or has all its elements are null. The new keyword in Java can be used to declare an empty array. Java allows us to determine whether an array is empty or not by determining whether its length is zero or whether all its entries have null values. Java also offers a variety of libraries to check Java empty arrays.
Generally, an empty array should satisfy the following two conditions
- The length of the array should be Zero (array_name.length=0).
- The array should not contain any elements.
When an array satisfies any of the above conditions, it is referred to as an empty array.
Checking whether an array is empty or not.
There are several methods to check whether an array is empty or not. Let us try to understand such methods.
Approach 1: Using 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 empty. (array_name.length=0)
Let us try to understand using a Java program.
Filename: Emptyarray1.java
public class Emptyarray1
{
public static void main(String[] args)
{
int array[] = {}; // Initialized the array no values.
if (array.length == 0)
{
System.out.println("The given array is an empty array");
}
else
{
System.out.println("The given array is not an empty array");
}
}
}
Output:
The given array is an empty array
Explanation: In the above program, we initialized the array with zero values. As the array contains no values, it returns array.length as 0, which is an empty array.
Approach 2: Null Check
Anarray can be considered an empty array java if the array shows the value NULL (array_name==null).
Filename: Emptyarray2.java
public class Emptyarray2
{
public static void main(String[] args) {
//Assigning the value null.
int array[] = null;
if (array == null)
{
System.out.println("The given array is an empty array");
}
else
{
System.out.println("The given array is not an empty array");
}
}
}
Output:
The given array is an empty array
Explanation: In the above program, we Initialized the given array with null. if condition will compare the given array with null. As the condition is true, it prints, "The given array is an empty array."
Approach 3: Using the Java library
Java programs have to include the java.util.Arrays class in order to check for empty arrays. The stream() method of the java.util.Arrays class can be used to use the allMatch() method to determine whether all of the array's null values are present.
Filename: Emptyarray3.java
import java.sql.Date;
import java.util.Arrays;
import java.util.Objects;
public class Emptyarray3 {
public static void main(String[] args) {
//assigning the value null.
Date dates[] =
{
null,
null,
null
};
boolean isEmpty = Arrays.stream(dates).allMatch(Objects::isNull);
if (isEmpty == true)
{
System.out.println("The given array is an empty array");
}
else
{
System.out.println("The given array is not an empty array");
}
}
}
Output:
The given array is an empty array
Explanation: In the example above, we declared an array of array and gave each position in the array the value null. Each element in the array will be looped through by the allMatch function and compared to null. The boolean value isEmpty will be set to false, and the loop will be broken if any item does not equal null. It will show "The input array is an empty array" if isEmpty is false. Otherwise, it will show "The input array is not an empty array."
Approach 4: Null Check on Elements
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 get a non-null element, then the array is not empty; otherwise, it is empty.
Filename: Emptyarray4.java
import java.util.Date;
public class Emptyarray4
{
public static void main(String[] args)
{
//assigning the value null.
Date array[] =
{
null,
null,
null
};
boolean isNotEmpty = false;
for (Date i: array)
{
if (i != null)
{
isNotEmpty = true;
break;
}
}
if (isNotEmpty == false)
{
System.out.println("The given array is an empty array");
}
else
{
System.out.println("The given array is not an empty array");
}
}
}
Output:
The given array is an empty array
Explanation: In the example above, we declared an array of array and gave each position in the array the value null. Each element in the dates will be looped through and checked against null using the for-condition Date i: array. The boolean variable isNotEmpty will be set to true, and the loop will be broken if any item does not equal null. It will display "The given array is an empty array" if isNotEmpty is false. Otherwise, it will display "The given array is not an empty array."