空ArrayList和具有null元素的ArrayList之间的区别? [英] Difference between an empty ArrayList and an ArrayList with null elements?

查看:1491
本文介绍了空ArrayList和具有null元素的ArrayList之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个解析JSON的REST服务编写一些验证器,我发现了一些听起来很奇怪的东西(我根本不是 JAVA专家 ) 。

I am coding some validators for a REST service which parse a JSON and I found out something that sounds wierd for me (I am not a JAVA expert at all).

考虑有两个 ArrayLists

Consider having two ArrayLists:

ArrayList<Object> list1 = new ArrayList<Object>();
ArrayList<Object> list2 = new ArrayList<Object>();

两个列表都包含 common 中的内容:它们完全为空(或充满空元素)。但如果我这样做:

Both lists have something in common: they are completely empty (or full of null elements). But if I do:

list1.add(null);

虽然两者都完全 ,<他们有完全不同的行为。要制作一些方法,结果 非常不同

Although both remain completely empty, they have completely different behaviors. And to make some methods the results are very different:

System.out.println(list1.contains(null));  //prints true!
System.out.println(list2.contains(null));  //prints false

System.out.println(CollectionUtils.isNotEmpty(list1));  //prints true
System.out.println(CollectionUtils.isNotEmpty(list2));  //prints false

System.out.println(list1.size());  //prints 1
System.out.println(list2.size());  //prints 0

做一些研究,并查看每种方法的实现,你可以确定这些差异的原因,但仍然不明白为什么区分这些列表是有效或有用的。

Doing some research, and looking at the implementation of each of these methods, you can determine the reason for these differences, but still do not understand why it would be valid or useful to differentiate between these lists.


  • 为什么 add(item) 不验证 item!= null

  • 为什么 包含(null) 如果列表中包含 false false

  • Why add(item) doesnt validate if item!=null ?
  • Why contains(null) says false if the list is full of nulls?

提前致谢!!!

编辑:

我大多同意答案,但我还不相信所有。这是方法remove的实现:

I am mostly agree whit the answers, but I'm not yet convinced all. This is the implementation of the method remove:

/**
 * Removes the first occurrence of the specified element from this list,
 * if it is present.  If the list does not contain the element, it is
 * unchanged.  More formally, removes the element with the lowest index
 * <tt>i</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
 * (if such an element exists).  Returns <tt>true</tt> if this list
 * contained the specified element (or equivalently, if this list
 * changed as a result of the call).
 *
 * @param o element to be removed from this list, if present
 * @return <tt>true</tt> if this list contained the specified element
 */
public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                return true;
            }
    } else {
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                fastRemove(index);
                return true;
            }
    }
    return false;
}


/*
 * Private remove method that skips bounds checking and does not
 * return the value removed.
 */
private void fastRemove(int index) {
    modCount++;
    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work
}

所以,现在如果我这样做:

So, now if i do:

ArrayList<Object> list = new ArrayList<Object>();
list.add(null);
System.out.println(list.contains(null)); //prints true!
list.remove(null);
System.out.println(list.contains(null));  //prints false!

我缺少什么?

what I'm missing?

推荐答案

包含 null 的列表是 NOT 。它包含 null 。列表允许包含null,因此如果需要,可以将null放入其中。

A list containing null is NOT empty. It contains null. Lists are allowed to contain null, so you can put null in it if you want.

这篇关于空ArrayList和具有null元素的ArrayList之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆