ネットワーク インターフェイスのリストを出力しようとしています (最終的には、何らかの文字列配列に格納します)。次のコードは、次の場合にのみインターフェイスのリストを出力します。
String[] networkInterfaces = new String[Collections.list(nets).size()];
行はありません。その 1 行が存在しない場合は、リスト全体が出力されます。
Enumeration<NetworkInterface> nets = null;
try {
nets = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
System.out.println(Collections.list(nets).size());
String[] networkInterfaces = new String[Collections.list(nets).size()];
for (NetworkInterface netint : Collections.list(nets)) {
System.out.println(netint.getName());
}
この質問にタグがなくて申し訳ありません。何が適切かわかりませんでした。なぜこれが発生するのですか?コレクションが ArrayList に保存されるように変更しました(これで問題ないようです)
ArrayList<NetworkInterface> netints = Collections.list(nets);
しかし、なぜ他の方法がうまくいかなかったのか、私はまだ興味があります。ありがとう :)