stack.isEmpty()和empty()


public class Stack<E> extends Vector<E>
可以看到Stack类继承了Vector类



这个是stack类里面的方法:
 /**
     * Tests if this stack is empty.
     *
     * @return  <code>true</code> if and only if this stack contains * no items; <code>false</code> otherwise.
     */
    public boolean empty() {
        return size() == 0;
    }
调用了vector的size方法

 

 /**
     * Returns the number of components in this vector.
     *
     * @return  the number of components in this vector
     */
    public synchronized int size() {
        return elementCount;
    }
vector的size方法返回elementCount

 

 

这个是Vector的方法(线程安全的):

/**
     * Tests if this vector has no components.
     *
     * @return  {@code true} if and only if this vector has
     *          no components, that is, its size is zero;
     *          {@code false} otherwise.
     */
    public synchronized boolean isEmpty() {
        return elementCount == 0;
    }

 

可以看到二者没有区别,都是看elementCount 是否为0


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM