Java: use Enumeration multiple times

依然范特西╮ 提交于 2019-11-28 08:57:23

问题


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?


回答1:


No that is not possible with Enumeration. On this subject, java doc says:

An object that implements the Enumeration interface generates a series of elements, one at a time.

Notice the one at a time which means that if you want to manipulate the Enumeration as a list then you will have to convert it. You can create a List when you first request the Enumeration. This way you could use and manipulate it however you wish.

Enumeration e = ...
ArrayList aList = Collections.list(e);


来源:https://stackoverflow.com/questions/10460534/java-use-enumeration-multiple-times

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!