简体   繁体   English

如何通过枚举列表“重置”迭代

[英]How to “reset” iteration through an Enumeration list

Enumeration<NetworkInterface> nets
        = NetworkInterface.getNetworkInterfaces();

out.print("List of all network interfaces on this machine:\n");

for (NetworkInterface netint : Collections.list(nets)) {
    out.printf("name:%s (%s)\n", netint.getName(), netint.getDisplayName());
}

I'm doing an assignment that requires me to list a bunch of information using NetworkInterface, but I'm not very familiar with Enumeration, so I'm having trouble. 我正在做一项作业,要求我使用NetworkInterface列出一堆信息,但是我对枚举并不十分熟悉,因此遇到了麻烦。

I went through some javadocs and managed to get this far. 我经历了一些javadocs并设法做到了这一点。 The problem is that I need to be able to go through the list multiple times. 问题是我需要能够多次浏览该列表。

For example, I was wanting to use another for loop to cycle through and list all interfaces that were currently up. 例如,我想使用另一个for循环来循环浏览并列出当前正在运行的所有接口。

Something like this: 像这样:

for (NetworkInterface netint : Collections.list(nets)) {
    if (netint.isUp()) {
       out.printf("name:%s (%s)\n", netint.getName(), netint.getDisplayName());
    }
}

However, any time I use a for loop after the first one I'm not getting any output. 但是,任何时候在第一个循环之后使用for循环时,都不会得到任何输出。 I have a feeling this has to do with my lack of understanding. 我觉得这与我缺乏理解有关。 I can't seem to find an explanation anywhere. 我似乎在任何地方都找不到解释。

An enumeration can't be reset. 枚举无法重置。 The reason your code doesn't work is because your first call to Collections.list(nets) "depleats" the nets enumeration, so the next call to Collections.list(nets) yields an empty collection. 您的代码不起作用的原因是,您对Collections.list(nets)首次调用会“消耗” nets枚举,因此下一次对Collections.list(nets)调用将产生一个空集合。

To avoid this collect the interfaces from the nets enumeration and iterate over the resulting list instead: 为了避免这种情况,请从nets枚举中收集接口,然后遍历结果列表:

List<NetworkInteface> list = Collections.list(nets);

// Iteration 1
for (NetworkInterface netint : list) {
    ...
}

// Iteration 2
for (NetworkInterface netint : list) {
    ...
}

You need to either: 您需要:

  • call NetworkInterface.getNetworkInterfaces() afresh before every loop (so that you get a new Enumeration object); 在每个循环之前再次调用NetworkInterface.getNetworkInterfaces() (这样您将获得一个新的Enumeration对象); or 要么
  • store the results of NetworkInterface.getNetworkInterfaces() in a list once , and then just iterate over that list. NetworkInterface.getNetworkInterfaces()的结果一次存储在一个列表中,然后在该列表上进行迭代。

To illustrate the second approach: 为了说明第二种方法:

List<NetworkInterface> nets = Collections.list(
                                 NetworkInterface.getNetworkInterfaces());

for (NetworkInterface netint : nets) {
   ...
}

for (NetworkInterface netint : nets) {
   ...
}

Try Iterator instead of Enumeration. 尝试使用Iterator而不是Enumeration。 Refer this docs, http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html 请参阅此文档, http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM