在Collections Framework中,我們有Iterator
和ListIterator
接口,它們用於迭代數據結構。它們爲迭代提供全部功能。但我想知道爲什麼JDK在Collections
類中有enumeration()
方法。爲什麼Collections.enumeration附帶枚舉()方法
任何人都可以向我解釋這一點嗎?
在Collections Framework中,我們有Iterator
和ListIterator
接口,它們用於迭代數據結構。它們爲迭代提供全部功能。但我想知道爲什麼JDK在Collections
類中有enumeration()
方法。爲什麼Collections.enumeration附帶枚舉()方法
任何人都可以向我解釋這一點嗎?
Iterator
和ListIterator
提供了完整的工具來遍歷集合。實際上,documentation for Iterator實質上表示Iterator
已經取代Enumeration
。每個集合都是Iterable
,因此可以使用Iterator
進行迭代。那麼爲什麼有一種方法Collections.enumeration
可以在該集合上生成Enumeration
?
原因是Enumeration
出現在原始的Java 1.0版本中,Iterator
直到在1.2中添加集合框架才被引入。因此,有一組代碼和API使用Enumeration
。 Collections.enumeration
方法被添加到(然後)新的基於集合的代碼以適應需要使用Enumeration
的舊代碼。
感謝你斯圖爾特標記 –
重新打開,因爲,雖然[其他問題](http://stackoverflow.com/questions/20707657/confusion-about-collections-enumeration-and-iterator-in-java)談論了很多關於'Collections'和'枚舉「,它實際上並沒有回答關於」Collection.enumeration「方法原因的具體問題。 –