Is an empty Arraylist (with nulls as its items) be considered as null? So, essentially would the below statement be true:
if (arrayList != null)
thanks
An empty collection isn't the same as null . An empty collection is actually a collection, but there aren't any elements in it yet. null means no collection exists at all.
In ArrayList, any number of null elements can be stored. While in HashMap, only one null key is allowed, but the values can be of any number.
null is a value that any ArrayList may contain as an element. Clearly, in that case the list has at least one element, and therefore is not empty. No matter what the initial state of a List , if an invocation of add(null) on that list completes normally then afterward the list is not empty.
To check if an ArrayList is empty, you can use ArrayList. isEmpty() method or first check if the ArrayList is null, and if not null, check its size using ArrayList. size() method. The size of an empty ArrayList is zero.
No.
An ArrayList can be empty (or with nulls as items) an not be null. It would be considered empty. You can check for am empty ArrayList with:
ArrayList arrList = new ArrayList(); if(arrList.isEmpty()) { // Do something with the empty list here. }
Or if you want to create a method that checks for an ArrayList with only nulls:
public static Boolean ContainsAllNulls(ArrayList arrList) { if(arrList != null) { for(object a : arrList) if(a != null) return false; } return true; }
arrayList == null
if there are no instance of the class ArrayList
assigned to the variable arrayList
(note the upercase for classes and the lowercase for variables).
If, at anytime, you do arrayList = new ArrayList()
then arrayList != null
because is pointing to an instance of the class ArrayList
If you want to know if the list is empty, do
if(arrayList != null && !arrayList.isEmpty()) { //has items here. The fact that has items does not mean that the items are != null. //You have to check the nullity for every item } else { // either there is no instance of ArrayList in arrayList or the list is empty. }
If you don't want null items in your list, I'd suggest you to extend the ArrayList class with your own, for example:
public class NotNullArrayList extends ArrayList{ @Override public boolean add(Object o) { if(o==null) throw new IllegalArgumentException("Cannot add null items to the list"); else return super.add(o); } }
Or maybe you can extend it to have a method inside your own class that re-defines the concept of "empty List".
public class NullIsEmptyArrayList extends ArrayList{ @Override public boolean isEmpty() if(super.isEmpty()) return true; else{ //Iterate through the items to see if all of them are null. //You can use any of the algorithms in the other responses. Return true if all are null, false otherwise. //You can short-circuit to return false when you find the first item not null, so it will improve performance. } }
The last two approaches are more Object-Oriented, more elegant and reusable solutions.
Updated with Jeff suggestion IAE instead of NPE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With