【问题标题】:Empty ArrayList equals null空 ArrayList 等于 null
【发布时间】:2011-03-13 07:58:14
【问题描述】:

一个空的 Arraylist(以 null 作为其项)是否被视为 null?所以,基本上下面的说法是正确的:

if (arrayList != null) 

谢谢

【问题讨论】:

    标签: java arrays arraylist


    【解决方案1】:

    没有。

    ArrayList 可以为空(或以 null 作为项),也可以不为 null。它会被认为是空的。您可以使用以下命令检查 am 空 ArrayList:

    ArrayList arrList = new ArrayList();
    if(arrList.isEmpty())
    {
        // Do something with the empty list here.
    }
    

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

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

    【讨论】:

    • 为什么不返回布尔值(原始类型)而不是布尔值(对象)?
    • 你能给我举个例子来说明如何创建一个空的arrayList吗?
    【解决方案2】:

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

    如果您在任何时候都执行arrayList = new ArrayList() 然后arrayList != null 因为指向ArrayList 类的一个实例

    如果您想知道列表是否为空,请执行

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

    如果您不希望列表中有空项目,我建议您使用自己的扩展 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);
        }
    }
    

    或者,也许您可​​以将其扩展为在您自己的类中拥有一个重新定义“空列表”概念的方法。

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

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

    更新 Jeff 建议 IAE 而不是 NPE。

    【讨论】:

    • 不错的建议!我会在 NotNullArrayList 示例中使用 IllegalArgumentException 而不是 NPE。
    【解决方案3】:

    不,这不起作用。您能做的最好的事情就是遍历所有值并自己检查它们:

    boolean empty = true;
    for (Object item : arrayList) {
        if (item != null) {
            empty = false;
            break;
        }
    }
    

    【讨论】:

    • 对于包含单个空值的列表,这将返回 false,所以可能不会。
    • @Mark 谢谢,已修复。似乎标题 'Empty ArrayList equals null' 把我扔了。
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

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

        空数组列表(以空值作为其项)

        其次,如果 ArrayList 为 EMPTY 表示它真的为空,它不能有 NULL 或 NON-NULL 的东西作为元素。

        第三,

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

        将打印错误。

        【讨论】:

          【解决方案7】:

          如果要检查数组是否包含具有空值的项目,请使用:

          private boolean isListOfNulls(ArrayList<String> stringList){
              for (String s: stringList)
                  if( s != null) return false;
              return true;
          }
          

          您可以将&lt;String&gt; 替换为您的ArrayList 的相应类型

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-12-13
            • 1970-01-01
            • 2011-03-31
            • 2013-06-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-22
            相关资源
            最近更新 更多