Java 空 ArrayList 等于 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3203664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:22:36  来源:igfitidea点击:

Empty ArrayList equals null

javaarraysarraylist

提问by Global Dictator

Is an empty Arraylist (with nulls as its items) be considered as null? So, essentially would the below statement be true:

空的 Arraylist(以空作为其项目)是否被视为空?所以,基本上下面的陈述是正确的:

if (arrayList != null) 

thanks

谢谢

采纳答案by Justin Niessner

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 可以为空(或以 null 作为项目)并且不为 null。它会被认为是空的。您可以使用以下命令检查是否为空 ArrayList:

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:

或者,如果您想创建一个检查仅包含空值的 ArrayList 的方法:

public static Boolean ContainsAllNulls(ArrayList arrList)
{
    if(arrList != null)
    {
        for(object a : arrList)
            if(a != null) return false;
    }

    return true;
}

回答by Iain Ward

No, because it contains items there must be an instance of it. Its items being null is irrelevant, so the statment ((arrayList) != null) == true

不,因为它包含项目,所以必须有一个实例。它的项为空是无关紧要的,所以语句 ((arrayList) != null) == true

回答by pakore

arrayList == nullif there are no instance of the class ArrayListassigned to the variable arrayList(note the upercase for classes and the lowercase for variables).

arrayList == null如果没有ArrayList分配给变量的类的实例arrayList(注意类的大写和变量的小写)。

If, at anytime, you do arrayList = new ArrayList()then arrayList != nullbecause is pointing to an instance of the class ArrayList

如果,在任何时候,你做arrayList = new ArrayList()那么arrayList != null因为所指向的类的实例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:

如果你不想在你的列表中出现空项,我建议你用你自己的来扩展 ArrayList 类,例如:

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.

后两种方法是更面向对象、更优雅和可重用的解决方案。

Updatedwith Jeff suggestion IAE instead of NPE.

更新了 Jeff 建议 IAE 而不是 NPE。

回答by krock

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;
    }
}

回答by dira

First of all, You can verify this on your own by writing a simple TestCase!

首先,您可以通过编写一个简单的 TestCase 自行验证这一点!

empty Arraylist (with nulls as its items)

空 Arraylist(以 null 作为其项目)

Second, If ArrayList is EMPTY that means it is really empty, it cannot have NULL or NON-NULL things as element.

其次,如果 ArrayList 是 EMPTY 这意味着它真的empty,它不能有 NULL 或 NON-NULL 东西作为元素。

Third,

第三,

 List list  = new ArrayList();
    list.add(null);
    System.out.println(list == null)

would print false.

会打印错误。

回答by Carl Manaster

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. nullis no list at all; it's therefore different from an empty list.

正如零是一个数字——只是一个代表无的数字——一个空列表仍然是一个列表,只是一个没有任何内容的列表。 null根本就没有清单;因此它不同于空列表。

Similarly, a list that contains null items is a list, and is notan 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.

类似地,包含空项的列表是一个列表,而不是一个空列表。因为里面有物品;这些项目本身为空并不重要。例如,一个包含三个空值的列表,没有别的:它的长度是多少?它的长度为 3。空列表的长度为零。而且,当然,null 没有长度。

回答by KarenAnne

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

您可以替换<String>为您的相应类型ArrayList