Stack 을 빈 것 으로 판단 하 는 방법의 empty () 와 isEmpty ()

오늘 알고리즘 을 쓸 때 본의 아니 게 스 택 이 비어 있 는 지 아 닌 지 를 판단 하 는 두 가지 방법 을 보 았 습 니 다. 그 전에 isEmpty () 를 사용 해 왔 습 니 다. empty () 를 사용 해 본 적 이 없 지만 둘 다 스 택 이 비어 있 는 지 아 닌 지 를 판단 할 수 있 을 거 라 고 생각 하지 않 았 습 니 다. 그래서 소스 코드 에 깊이 들 어가 서 연 구 했 습 니 다. java. util. 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;
    }
	 /**
     * Returns the number of components in this vector.
     *
     * @return  the number of components in this vector
     */
    public synchronized int size() {
        return elementCount;
    }

java. util. Stack 클래스 중:
/**
     * Tests if this stack is empty.
     *
     * @return  true if and only if this stack contains
     *          no items; false otherwise.
     */
    public boolean empty() {
        return size() == 0;
    }
class Stack extends Vector 자바 util. Stack 류 에서 size () 방법 은 자바 util. Vector 의 size () 방법 을 사용 하 는 것 입 니 다. 그러면 소스 코드 에서 볼 때 empty () 와 isEmpty () 는 본질 적 인 차이 가 없습니다.
스 택 이 비어 있 는 지 판단 할 때 stack. empty () 와 stack. isEmpty () 를 사용 하면 두 가지 방법 이 존재 하 는 클래스 만 다 를 수 있 습 니 다.

좋은 웹페이지 즐겨찾기