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