public static List getIPs(String ip1, String ip2){ String ipOne[] = ip1.split("\\."); String ipTwo[] = ip2.split("\\.");
int intIpOne[] = new int[4]; int intIpTwo[] = new int[4]; for(int i = 0; i < 4; i ++){ intIpOne[i] = Integer.parseInt(ipOne[i]); intIpTwo[i] = Integer.parseInt(ipTwo[i]); }
List result = new ArrayList(); for(int i = intIpOne[0]; i <= intIpTwo[0]; i++){ for(int j = intIpOne[1]; j <= intIpTwo[1]; j++){ for(int k = intIpOne[2]; k <= intIpTwo[2]; k++){ for(int l = intIpOne[3]; l <= intIpTwo[3]; l++){ StringBuffer ip = new StringBuffer(); ip.append(i).append(".") .append(j).append(".") .append(k).append(".") .append(l);
result.add(ip.toString()); } } } }
return result; } } 这笨办法,你说的那个懒得去写了,你自己去慢慢研究
我表示楼上的是错了,我写了一个,如下: public static Object[] GET_IP_ARR(String ipfrom, String ipto) { ArrayList<String> ips = new ArrayList<String>(); String[] ipfromd = ipfrom.split("\\."); String[] iptod = ipto.split("\\."); int[] int_ipf = new int[4]; int[] int_ipt = new int[4]; for (int i = 0; i < 4; i++) { int_ipf[i] = Integer.parseInt(ipfromd[i]); int_ipt[i] = Integer.parseInt(iptod[i]); } for (int A = int_ipf[0]; A <= int_ipt[0]; A++) { for (int B = int_ipf[1]; B <= (A == int_ipt[0] ? int_ipt[1] : 255); B++) { for (int C = int_ipf[2]; C <= (B == int_ipt[1] ? int_ipt[2] : 255); C++) { for (int D = int_ipf[3]; D <= (C == int_ipt[2] ? int_ipt[3] : 255); D++) { ips.add(new String(A+"."+B+"."+C+"."+D)); } } } } return ips.toArray(); }
。。楼上发了个错误的版本。。重新发 public static Object[] GET_IP_ARR(String ipfrom, String ipto) { ArrayList<String> ips = new ArrayList<String>(); String[] ipfromd = ipfrom.split("\\."); String[] iptod = ipto.split("\\."); int[] int_ipf = new int[4]; int[] int_ipt = new int[4]; for (int i = 0; i < 4; i++) { int_ipf[i] = Integer.parseInt(ipfromd[i]); int_ipt[i] = Integer.parseInt(iptod[i]); } for (int A = int_ipf[0]; A <= int_ipt[0]; A++) { for (int B = (A == int_ipf[0] ? int_ipf[1] : 0); B <= (A == int_ipt[0] ? int_ipt[1] : 255); B++) { for (int C = (B == int_ipf[1] ? int_ipf[2] : 0); C <= (B == int_ipt[1] ? int_ipt[2] : 255); C++) { for (int D = (C == int_ipf[2] ? int_ipf[3] : 0); D <= (C == int_ipt[2] ? int_ipt[3] : 255); D++) { ips.add(new String(A + "." + B + "." + C + "." + D)); } } } } return ips.toArray(); }
* @param args
*/
public static void main(String[] args) {
String ip1 = "192.168.5.1";
String ip2 = "192.168.6.254";
for(Iterator ipItr = getIPs(ip1, ip2).iterator(); ipItr.hasNext();){
System.out.println(ipItr.next());
} }
public static List getIPs(String ip1, String ip2){ String ipOne[] = ip1.split("\\.");
String ipTwo[] = ip2.split("\\.");
int intIpOne[] = new int[4];
int intIpTwo[] = new int[4];
for(int i = 0; i < 4; i ++){
intIpOne[i] = Integer.parseInt(ipOne[i]);
intIpTwo[i] = Integer.parseInt(ipTwo[i]);
}
List result = new ArrayList();
for(int i = intIpOne[0]; i <= intIpTwo[0]; i++){
for(int j = intIpOne[1]; j <= intIpTwo[1]; j++){
for(int k = intIpOne[2]; k <= intIpTwo[2]; k++){
for(int l = intIpOne[3]; l <= intIpTwo[3]; l++){
StringBuffer ip = new StringBuffer();
ip.append(i).append(".")
.append(j).append(".")
.append(k).append(".")
.append(l);
result.add(ip.toString());
}
}
}
}
return result;
}
}
这笨办法,你说的那个懒得去写了,你自己去慢慢研究
public static Object[] GET_IP_ARR(String ipfrom, String ipto) {
ArrayList<String> ips = new ArrayList<String>();
String[] ipfromd = ipfrom.split("\\.");
String[] iptod = ipto.split("\\.");
int[] int_ipf = new int[4];
int[] int_ipt = new int[4];
for (int i = 0; i < 4; i++) {
int_ipf[i] = Integer.parseInt(ipfromd[i]);
int_ipt[i] = Integer.parseInt(iptod[i]);
}
for (int A = int_ipf[0]; A <= int_ipt[0]; A++) {
for (int B = int_ipf[1]; B <= (A == int_ipt[0] ? int_ipt[1] : 255); B++) {
for (int C = int_ipf[2]; C <= (B == int_ipt[1] ? int_ipt[2]
: 255); C++) {
for (int D = int_ipf[3]; D <= (C == int_ipt[2] ? int_ipt[3]
: 255); D++) {
ips.add(new String(A+"."+B+"."+C+"."+D));
}
}
}
}
return ips.toArray();
}
ArrayList<String> ips = new ArrayList<String>();
String[] ipfromd = ipfrom.split("\\.");
String[] iptod = ipto.split("\\.");
int[] int_ipf = new int[4];
int[] int_ipt = new int[4];
for (int i = 0; i < 4; i++) {
int_ipf[i] = Integer.parseInt(ipfromd[i]);
int_ipt[i] = Integer.parseInt(iptod[i]);
}
for (int A = int_ipf[0]; A <= int_ipt[0]; A++) {
for (int B = (A == int_ipf[0] ? int_ipf[1] : 0); B <= (A == int_ipt[0] ? int_ipt[1]
: 255); B++) {
for (int C = (B == int_ipf[1] ? int_ipf[2] : 0); C <= (B == int_ipt[1] ? int_ipt[2]
: 255); C++) {
for (int D = (C == int_ipf[2] ? int_ipf[3] : 0); D <= (C == int_ipt[2] ? int_ipt[3]
: 255); D++) {
ips.add(new String(A + "." + B + "." + C + "." + D));
}
}
}
}
return ips.toArray();
}