list.isEmpty()和list.size()>0时间复杂度

来源:互联网 发布:希望我们都好好的 知乎 编辑:程序博客网 时间:2024/05/17 09:05

List的源码如下所示:(关于Empty和size)

  /**     * Returns the number of elements in this list.     *     * @return the number of elements in this list     */    public int size() {        return size;    }     /**     * Returns <tt>true</tt> if this list contains no elements.     *     * @return <tt>true</tt> if this list contains no elements     */    public boolean isEmpty() {        return size == 0;    }

如果我要查size时间复杂度是o(n) ,如果是empty o(1);所以判断集合是否为空一般用list.isEmpty()来判断

阅读全文
0 0