Return type of iterator() method in java

前端 未结 6 1484
耶瑟儿~
耶瑟儿~ 2021-01-03 07:46

I am a new-comer in Java and in process of learning. I need a an answer of following question supported with valid theory. Consider the following line-

Iterat         


        
6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 08:48

    I want to know what is here the return type of al.iterator()

    You shouldn't care. It doesn't matter what particular implementation of Iterator the method returns, only that it returns an Iterator. You can call next, hasNext, and remove, or iterate with a for-each loop, without knowing that it happens to be an ArrayList.Itr or whatever the implementation is.

    Suppose the Java developers working on the ArrayList want to rename their iterator implementation to ArrayListIterator instead of whatever they're calling it now. If your code relied on knowing the exact Iterator implementation, you would have to change your code just to handle an ArrayList-internal change you shouldn't even see.

提交回复
热议问题