Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chapter 13] P.419 Iterator.remove() #26

Open
kimjinwook1 opened this issue Nov 16, 2022 · 2 comments
Open

[Chapter 13] P.419 Iterator.remove() #26

kimjinwook1 opened this issue Nov 16, 2022 · 2 comments
Assignees

Comments

@kimjinwook1
Copy link
Contributor

image

Iterator.remove()에는 예외를 던지는 구현부만 존재하는데 어떻게 remove가 작동하는 건가요?

아래는 예시 코드입니다.

    public static void main(String[] args) {
        String removeElem = "Perl";
        List<String> myList = new ArrayList<String>();
        myList.add("Java");
        myList.add("Unix");
        myList.add("Oracle");
        myList.add("C++");
        myList.add("Perl");
        System.out.println("Before remove:");
        System.out.println(myList);

        Iterator<String> itr = myList.iterator();
        while(itr.hasNext()){
            if(removeElem.equals(itr.next())){
                itr.remove();
            }
        }
        System.out.println("After remove:");
        System.out.println(myList);
    }
@kimjinwook1
Copy link
Contributor Author

예제 코드에서는 myList의 구현체가 ArrayList<>; 이므로 ArrayList의 remove가 작동하네요.
각 컬렉션 구현체에 메서드가 동작하는 것 같습니다!

@jaeminkim90
Copy link
Contributor

이번 장 후반부에 설명하고 있는 Method 호출 우선순위의 내용이 적용되네요!
image

확인해보니 myList를 Iterator로 업캐스팅하고 있기 때문에 마치 Iterator 타입의 디폴트 메서드를 호출할 거 같지만 JVM에서 호출하는 포인터 순서는 최하위 구현체부터 호출을 하네요! 포인터 경로를 따라 호출하면서 동일한 메서드 Override가 있으면 그걸 우선적으로 사용하는 거 같습니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants