Check if array is null java

how to check for an empty array java [duplicate]

I wanted to know if this code is valid for checking whether an array is empty, or should I check for null?

if(arrayName=<>) System.out.println("array empty"); else System.out.println("array not empty"); 

«I wanted to know if this code is valid for checking whether an array is empty» — the code you posted doesn’t even compile. In the future, it’s probably faster to just try compiling your code rather than asking people on the internet to look at it.

6 Answers 6

I would consider using ArrayUtils.is empty by adding Apache Commons Lang from here http://commons.apache.org/proper/commons-lang/download_lang.cgi

The big advantage is this will null check the array for you in a clean and easily readible way.

if (ArrayUtils.isEmpty(arrayName) < System.out.printLn("Array empty"); >else

In array class we have a static variable defined «length», which holds the number of elements in array object. You can use that to find the length as:

if(arrayName.length == 0) System.out.println("array empty"); else System.out.println("array not empty"); 

As poited out in the other answers, the length property of Array will give the length of the array. But it is always recommended to check if the array is null before doing any operation on it or else it will throw NullPointerException if the array is null .

 if (array != null) < if (array.length == 0) System.out.println("Empty Array Size=0"); else System.out.println("Array Not Empty - Size = " + array.length); >else System.out.println("array is null"); > 

No, because array literals don’t work that way. Replace arrayName=<> with arrayName.length==0 , and it should work.

Читайте также:  Kivy python android приложение

To check empty try like this:

proudandhonour’s answer is on the right track but won’t work for all possible values of arrayName , specifically that case in which arrayName hasn’t been defined and is null . In that case, the code:

if(arrayName.length == 0) System.out.println("array empty"); else System.out.println("array not empty"); 

will fail with a NullPointerException. TimeTravel’s answer correctly tests for this case and correctly handles all possible values for arrayName . The only downside is that his code is more verbose than it needs to be.

Java provides short circuit evaluation of Boolean expressions. Specifically the result of (false && xx) is false for all possible values of xx . Therefore when evaluating the first operand of a Boolean && operator, the JVM will ignore the 2nd operand if the 1st evaluates to false.

Exploiting this, we can write:

if (arrayName != null && arrayName.length > 0) < System.out.println("The array length >0"); > else

There is also the ternary operator which provides a mechanism for inlining if-then-else expressions. This can improve readability in some circumstances:

System.out.println((arrayName == null) ? "The arrayName variable is null" : (arrayName.length < 1) ? "The array length is zero" : "The array length is " + String.valueOf(arrayName.length) ); 

Источник

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.

Источник

Check Whether an Array Is Null/Empty in Java

Check Whether an Array Is Null/Empty in Java

  1. Null Array in Java
  2. Array Contains Null Values
  3. Empty Array in Java
  4. Check Array Null Using Apache Commons Library in Java
  5. Check Array Null Using Java 8

This tutorial introduces how to check whether an array is null or empty in Java and also lists some example codes to understand the null checking process.

Null Array in Java

In Java, an array is an object that holds similar types of data. It can be null only if it is not instantiated or points to a null reference.

In this example, we have created two arrays. The array arr is declared but not instantiated. It does not hold any data and refers to a null reference (default value) assigned by the compiler. The array arr2 is declared and explicitly assigned to null to create a null array.

We can use this example to check whether the array is null or not.

public class SimpleTesting    String[] arr;  String[] arr2 = null;   public static void main(String[] args)   SimpleTesting obj = new SimpleTesting();  if(obj.arr == null)   System.out.println("The array is null");  >  if(obj.arr2 == null)   System.out.println("The array2 is null");  >  > > 
The array is null The array2 is null 

Array Contains Null Values

This is the second scenario where an array contains null values. In that case, we can consider an array to be null.

Suppose, we have an array of string that can contain 5 elements. Since the array is not initialized then it holds null (default value) assigned by the compiler.

public class SimpleTesting    String[] arr = new String[5];   public static void main(String[] args)   boolean containNull = true;  SimpleTesting obj = new SimpleTesting();  for(int i = 0; iobj.arr.length; i++)   if(obj.arr[i] != null)   containNull = false;  break;  >  >  if(containNull)   System.out.println("Array is null");  >  > > 

Empty Array in Java

An array is empty only when it contains zero(0) elements and has zero length. We can test it by using the length property of the array object.

public class SimpleTesting    String[] arr = new String[0];   public static void main(String[] args)   SimpleTesting obj = new SimpleTesting();  if(obj.arr.length == 0)   System.out.println("The array is Empty");  >  > > 

Check Array Null Using Apache Commons Library in Java

If you are working with Apache then use ArrayUtils class to check whether an array is empty. The ArrayUtils class provides a method isEmpty() which returns a boolean value either true or false. For more info about apache library visit here.

import org.apache.commons.lang3.ArrayUtils;  public class SimpleTesting    String[] arr = new String[0];   public static void main(String[] args)   SimpleTesting obj = new SimpleTesting();  Boolean isEmpty = ArrayUtils.isEmpty(obj.arr);  if(isEmpty)   System.out.println("Array is Empty");  >  > > 

Check Array Null Using Java 8

If you are working with Java 8 or higher version then you can use the stream() method of Arrays class to call the allMatch() method to check whether array contains null values or not.

This is the case when array contains null values.

import java.util.Arrays; import java.util.Objects;  public class SimpleTesting    String[] arr = new String[10];   public static void main(String[] args)   SimpleTesting obj = new SimpleTesting();  Boolean containNull = Arrays.stream(obj.arr).allMatch(Objects::nonNull);  if(!containNull)   System.out.println("Array is null");  >  > > 

Related Article - Java Array

Источник

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