Return type of iterator() method in java

前端 未结 6 1504
耶瑟儿~
耶瑟儿~ 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:49

    You can define the return type of your Iterator beforehand. As you declare the iterator, you can (and should) define the type of your list items like this:

    Iterator itr = al.iterator();

    So when you access itr you don't have to cast to file, e.g. (File) itr.next() but can directly use it in your while loop:

    while (it.hasNext()) { File f = itr.next() }

    Your compiler now won't complain when using next().

提交回复
热议问题