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;
}
No, this will not work. The best you will be able to do is to iterate through all values and check them yourself:
boolean empty = true;
for (Object item : arrayList) {
if (item != null) {
empty = false;
break;
}
}
Just as zero is a number - just a number that represents none - an empty list is still a list, just a list with nothing in it. null
is no list at all; it's therefore different from an empty list.
Similarly, a list that contains null items is a list, and is not an empty list. Because it has items in it; it doesn't matter that those items are themselves null. As an example, a list with three null values in it, and nothing else: what is its length? Its length is 3. The empty list's length is zero. And, of course, null doesn't have a length.
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.
No, because it contains items there must be an instance of it. Its items being null is irrelevant, so the statment ((arrayList) != null) == true
First of all, You can verify this on your own by writing a simple TestCase!
empty Arraylist (with nulls as its items)
Second, If ArrayList is EMPTY that means it is really empty, it cannot have NULL or NON-NULL things as element.
Third,
List list = new ArrayList();
list.add(null);
System.out.println(list == null)
would print false.
If you want to check whether the array contains items with null values, use this:
private boolean isListOfNulls(ArrayList<String> stringList){
for (String s: stringList)
if( s != null) return false;
return true;
}
You could replace <String>
with the corresponding type for your ArrayList
Source: Stackoverflow.com