I had a little program that relied on getting some information from the local Network Interfaces.
Then, I needed to add some functionality, which needed that information again.
I didn't want to touch my tested and highly used methods. So, under the pressure of time, I decided to reuse the method, effectively translating the NetworkInterface Enumeration into an ArrayList twice (not my proudest coding moment!).
Upon testing, I noticed it doesn't really work!
Sequence of actions:
- I grab the NetworkInterface Enumeration from the OS once, into a class field
- myMethod translates the NetworkInterface Enumeration into an ArrayList, does some analysis and returns some information, to do something
- myMethod translates the NetworkInterface Enumeration into an ArrayList, does some analysis and returns some information, to do something else
I notice, on the second translation, the result was zero!
In debug, I notice that my Enumeration has a field "i".
That field: first time, it equaled 55 (the count of my IFs), second time, it equaled 0...
I provide a small piece of code that should reproduce it for your debugging pleasure (I am using Java 8):
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
public class CollectionsListDemo {
public static void main(String[] args) {
ArrayList<NetworkInterface> netFacesArList1;
ArrayList<NetworkInterface> netFacesArList2;
try {
Enumeration<NetworkInterface> netFaces = NetworkInterface.getNetworkInterfaces();
netFacesArList1 = Collections.list(netFaces);
netFacesArList2 = Collections.list(netFaces);
} catch (Exception e) {
throw new RuntimeException("whatever");
}
System.out.println(netFacesArList1.size()); // 55, on my machine
System.out.println(netFacesArList2.size()); // 0
}
}
Basically, what I want to know is if there's a practical reason Collections.list() works this way... Or it's just a limitation... Or I am using wrong?
For now, I am just going to take this translation out, into a separate ArrayList, which I think I can read in different methods, without it remembering stuff from previous reads...
question from:
https://stackoverflow.com/questions/65852332/java-why-is-java-util-collections-listenumeration-destructive 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…