더북(TheBook)

2.8 직접 만들지 말고 자바 API 사용하기

class Inventory {

    private List<Supply> supplies = new ArrayList<>();

    int getQuantity(Supply supply) { 
        if (supply == null) {
            throw new NullPointerException("supply must not be null"); 
        }

        int quantity = 0;
        for (Supply supplyInStock : supplies) {
            if (supply.equals(supplyInStock)) {
                quantity++;
            } 
        }
        return quantity;

    }
}

프로그래밍 초창기에는 전부 스스로 만들어야 했습니다. C 언어에서는 char[]String을 생성하거나 리스트도 스스로 구현해야 했죠. 모든 자료 구조와 알고리즘 유형을 이렇게 만들어야 했습니다. 훈련으로는 더할 나위 없지만 시간이 많이 걸리고 오류가 발생하기 쉬웠습니다.