日期:2014-05-20  浏览次数:20670 次

请教linkedlist源代码,有点没明白什么意思。

public int indexOf(Object o) {
        int index = 0;
        if (o==null) {
            for (Entry e = header.next; e != header; e = e.next) {
                if (e.element==null)
                    return index;
                index++;
            }
        } else {
            for (Entry e = header.next; e != header; e = e.next) {
                if (o.equals(e.element))
                    return index;
                index++;
            }
        }
        return -1;
    }

if (o==null) {
            for (Entry e = header.next; e != header; e = e.next) {
                if (e.element==null)
                    return index;
                index++;
            }
        } 
这一部分什么意思?o==null?

------解决方案--------------------
o==null的时候分开来讨论因为o是null的话没有办法调用方法(会报错),所以也不能用下面的
o.equals(e.element)

------解决方案--------------------


http://blog.csdn.net/cdguogang/article/details/7294294

之所以 使用o==null,可能是因为null也可以放入集合中,然后可以返回这个null在集合中的索引下标
------解决方案--------------------
是没什么意思,linkedlist就是由node环组成的,每个Node是这样的
 E item;
        Node<E> next;
        Node<E> prev;
item自然可以放null了
------解决方案--------------------
我实际开发经验不多,感觉没有什么用
------解决方案--------------------
不管放null进去有什么用,但是作为API,他得考虑到这个情况,要不然万一调用者不小心把null放进去了,怎么处理呢?对吧
------解决方案--------------------
就理解为对null的支持呗,跟ArrayList也支持null一样咯。。。
------解决方案--------------------
引用:
支持null我可以理解,就是想问问各位,这个在实际开发中有什么用途吗?感觉好像没啥用啊


应该实际中没啥用,因为一般情况下毕竟没有人会把null放进去的。。