如果每个数组中只有一个条目,则以下工作正常,但否则会导致IllegalArgumentException
:
String[] defaultGWs = {"10.0.0.138, 10.0.0.138"};
String[] interfacesNetmasks = {"255.255.255.0, 255.255.255.0"};
for (String defaultGW: defaultGWs) {
for (String interfacesNetmask: interfacesNetmasks) {
SubnetInfo subnetInfo = (new SubnetUtils(defaultGW, interfacesNetmask).getInfo());
// CIDR signature
String cidrSignature = subnetInfo.getCidrSignature();
System.out.println("CIDR Signature: " + cidrSignature);
}
例外:
Exception in thread "main" java.lang.IllegalArgumentException: Could not parse [255.255.255.0, 255.255.255.0]
at org.apache.commons.net.util.SubnetUtils.toInteger(SubnetUtils.java:287)
at org.apache.commons.net.util.SubnetUtils.toCidrNotation(SubnetUtils.java:360)
at org.apache.commons.net.util.SubnetUtils.<init>(SubnetUtils.java:62)
at GetSubnetInfoJava.main(GetSubnetInfoJava.java:13)
如何并行遍历大量网关和网络掩码,以便在每次迭代时都可以生成新的subnetInfo对象?
答案 0 :(得分:2)
String[] defaultGWs = {"10.0.0.138, 10.0.0.138"};
String[] interfacesNetmasks = {"255.255.255.0, 255.255.255.0"};
这些是问题所在。声明一个字符串数组,每个字符串只有一个条目。将其更改为
String[] defaultGWs = {"10.0.0.138", "10.0.0.138"};
String[] interfacesNetmasks = {"255.255.255.0","255.255.255.0"};
现在你有2个带有2个条目的数组,你可以正确迭代
答案 1 :(得分:2)
您当前的代码实际上为每个defaultGW遍历所有 interfaceNetmasks,所以如果您想要并行浏览数组:
保持其余代码相同 - 除了修复数组项中的引号。
String[] defaultGWs = {"10.0.0.138", "10.0.0.138"};
String[] interfacesNetmasks = {"255.255.255.0", "255.255.255.0"};
for (int i = 0; i < defaultGWs.length; i++) {
SubnetInfo subnetInfo = (new SubnetUtils(defaultGWs[i], interfacesNetmasks[i]).getInfo());
// CIDR signature
String cidrSignature = subnetInfo.getCidrSignature();
System.out.println("CIDR Signature: " + cidrSignature);
}
答案 2 :(得分:1)
变化:
String[] defaultGWs = {"10.0.0.138, 10.0.0.138"};
String[] interfacesNetmasks = {"255.255.255.0, 255.255.255.0"};
要:
String[] defaultGWs = {"10.0.0.138", "10.0.0.138"};
String[] interfacesNetmasks = {"255.255.255.0", "255.255.255.0"};