![](/img/trans.png)
[英]Java: Why is java.util.Collections.list(Enumeration) destructive?
[英]Why Collections.enumeration comes with enumeration() method
在Collections Framework中,我們具有Iterator
和ListIterator
接口,這些接口用於迭代數據結構。 它們提供了完整的迭代功能。 但是我想知道為什么JDK在Collections
類上有一個enumeration()
方法。
誰能向我解釋這一點?
Iterator
和ListIterator
提供了用於遍歷集合的完整工具。 確實, Iterator的文檔從本質上說是Iterator
取代了Enumeration
。 每個集合都是一個Iterable
,因此可以使用Iterator
。 那么為什么會有方法Collections.enumeration
對該Collections.enumeration
產生Enumeration
?
原因是在原始Java 1.0版本中存在Enumeration
,並且直到在1.2中添加了collections框架后才引入Iterator
。 因此,有大量使用Enumeration
的代碼和API。 添加了Collections.enumeration
方法以(然后)使新的基於集合的代碼適應需要使用Enumeration
舊代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.