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

查看:31
本文介绍了空 ArrayList 和具有空元素的 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>();

两个列表都共同:它们完全空(或充满空元素).但如果我这样做:

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 ?
  • 如果列表充满nulls,为什么contains(null)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!

我缺少什么?

推荐答案

包含 null 的列表是 NOT empty.它包含 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 和具有空元素的 ArrayList 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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