4

I work with DefaultMutableTreeNode, and it has methods depthFirstEnumeration(), breadthFirstEnumeration() and children() that return Enumeration of tree nodes.

I need to use the returned Enumeration multiple times, but I can't find any method like reset() in Enumeration. It seems like I only can get all the elements just once, and then call depthFirstEnumeration() again to get new Enumeration, it seems not to be a good solution.

Of course, I can get all the elements from Enumeration and convert it to any another reusable representation, but is there really a way to use Enumeration multiple times?

4

1 回答 1

6

不,这是不可能的Enumeration。在这个问题上,java doc 说:

实现 Enumeration 接口的对象一次生成一系列元素。

一次注意一个,这意味着如果您想将其Enumeration作为列表进行操作,则必须对其进行转换。您可以在第一次请求时创建一个列表Enumeration。这样您就可以随心所欲地使用和操作它。

Enumeration e = ...
ArrayList aList = Collections.list(e);
于 2012-05-05T09:34:25.983 回答