随笔-1  评论-0  文章-2  trackbacks-0
Iterator 和 Enumeration 有什么不同的地方?
One key difference between an enumerator and an iterator is that the iterator will fail if the underlying collection changes.
The Java 1.0/1.1 version of the iterator chose to invent a new name, “enumeration,” instead of using a term that everyone was already familiar with. The Enumeration interface is smaller than Iterator, with only two methods, and it uses longer method names: boolean hasMoreElements( ) produces true if this enumeration contains more elements, and Object nextElement( ) returns the next element of this enumeration if there are any more (otherwise it throws an exception).
Enumeration is only an interface, not an implementation, and even new libraries sometimes still use the old Enumeration, which is unfortunate but generally harmless. Even though you should always use Iterator when you can in your own code, you must be prepared for libraries that want to hand you an Enumeration.
posted on 2005-04-05 11:14 天下无名 阅读(396) 评论(0)  编辑  收藏 所属分类: 软件碎屑

只有注册用户登录后才能发表评论。


网站导航: