简体   繁体   English

Java类ArrayList如何返回迭代器对象?

[英]How does the Java class ArrayList return an Iterator Object?

I know that you get it by calling the iterator() method on the ArrayList that you created, but what does that method look like? 我知道您可以通过在创建的ArrayList上调用iterator()方法来获得它,但是该方法是什么样的?

Since Iterator is only an interface defined in Java, I am not sure how the ArrayList passes back a concrete implementation of the Iterator? 由于Iterator只是用Java定义的接口,因此我不确定ArrayList如何传递回Iterator的具体实现吗?

I know how to implement these myself for my own classes...I want to know how Java's ArrayList does it...maybe there is a concrete Iterator Class in the standard Library I don't know about? 我知道如何为自己的类自己实现这些...我想知道Java的ArrayList是如何实现的...也许我不知道的标准库中有具体的Iterator类?

You can find it out youself 你自己就能找到

System.out.println(new ArrayList().iterator().getClass());

I get class java.util.ArrayList$Itr probably you too. 我可能也得到class java.util.ArrayList$Itr $ sign mean inner class (static or not). $符号表示内部类(是否静态)。 And if we go inside source we'll see 如果我们进入源代码,我们将看到

public Iterator<E> iterator() {
    return new Itr();
}

And later in this file 然后在这个文件中

private class Itr implements Iterator<E> { ... }

The ArrayList class internally has an implementation of Iterator . ArrayList类在内部具有Iterator的实现。 Either that, or it has an internal method where it constructs an Iterator implementation from another class. 要么,要么它具有一个内部方法,该方法从另一个类构造一个Iterator实现。 The reason I don't know for sure is because the only way to know is to look through the source code, which I haven't done. 我不确定的原因是因为唯一的了解方法就是浏览源代码,而我还没有做过。 The point of having an interface is that you don't need to know the internal implementation. 拥有接口的关键是您不需要了解内部实现。 You just know what the Iterator interface is capable of doing - which is defined by the interface spec. 您只知道Iterator接口的功能-接口规范定义了该接口。

And yes, somewhere there is a concrete implementation of the Iterator interface. 是的,在某处Iterator接口的具体实现。 The ArrayList class is using the constructor from such an implementation, creating the object, and returning it to you. ArrayList类使用此类实现中的构造函数,创建对象并将其返回给您。

The iterator looks like this 迭代器看起来像这样

Iterator<T> iterator() {
  return new Iterator<T>(this);
}

It return a new iterator every time. 每次返回一个新的迭代器。
Note you should do something like this in you code to not end up in an endless for loop 注意,您应该在代码中执行类似的操作,以免最终陷入无穷的for循环

Iterator iterator = myCollections.iterator();

Use this iterator object to loop over you collection. 使用此迭代器对象可以遍历您的集合。 If you do 如果你这样做

while(myCollections.iterator().hasNext) {
   System.out.println(myCollections.iterator().next());
}

It will be an endless loop just printing the first object always. 只打印第一个对象将是一个无休止的循环。


EDIT: 编辑:


To answer your question it comes from AbstractList which has an concrete implementation for it. 为了回答您的问题,它来自AbstractList ,它具有具体的实现。

ArrayList has internally implemented the iterator as a nested class. ArrayList在内部将迭代器实现为嵌套类。

private class Itr implements Iterator<E> {
    int cursor;       // index of next element to return
    int lastRet = -1; // index of last element returned; -1 if no such
    int expectedModCount = modCount;

    public boolean hasNext() {
       return cursor != size;
   }

and then it return object of it by the method iterator() 然后通过iterator()方法返回它的对象

 public Iterator<E> iterator() {
    return new Itr();
 }

you can refer the sources code for better understanding ArrayList Source code 您可以参考源代码以更好地理解ArrayList源代码

hope this ll help 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM