处理枚举< T>作为迭代器< T> [英] Treat Enumeration<T> as Iterator<T>
本文介绍了处理枚举< T>作为迭代器< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个实现了 Enumeration< T>
接口的类,但是Java的foreach循环需要 Iterator< T> $ c $接口。 Java标准库中是否存在
Enumeration
到 Iterator
适配器?
Enumeration 到否则不兼容的
$ p $
Iterator
。 Apache commons-collections有 EnumerationIterator
。用法是:$ p $
Iterator iterator = new EnumerationIterator(enumeration);
I have a class that implements the Enumeration<T>
interface, but Java's foreach loop requires the Iterator<T>
interface. Is there an Enumeration
to Iterator
Adapter in Java's standard library?
解决方案
You need a so called "Adapter", to adapt the Enumeration
to the otherwise incompatible Iterator
. Apache commons-collections has EnumerationIterator
. The usage is:
Iterator iterator = new EnumerationIterator(enumeration);
这篇关于处理枚举< T>作为迭代器< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文