java代码定义ip java代码定义json数据

IP 地址排序Javapackage src;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Ip { //定义ip bean
int ip1;
int ip2;
int ip3;
int ip4;
public Ip(int ip1, int ip2, int ip3, int ip4) {
this.ip1 = ip1;
this.ip2 = ip2;
this.ip3 = ip3;
this.ip4 = ip4;
}
public int getIp1() {
return ip1;
}
public void setIp1(int ip1) {
this.ip1 = ip1;
}
public int getIp2() {
return ip2;
}
public void setIp2(int ip2) {
this.ip2 = ip2;
}
public int getIp3() {
return ip3;
}
public void setIp3(int ip3) {
this.ip3 = ip3;
}
public int getIp4() {
return ip4;
}
public void setIp4(int ip4) {
this.ip4 = ip4;
}
}
class ComparatorUser implements Comparator {
public int compare(Object arg1, Object arg2) {
Ip ip1 = (Ip) arg1;
Ip ip2 = (Ip) arg2;
int flag = ip1.getIp1() - ip2.getIp1();
if (flag == 0) { //如果一段相等用二段排序,以此类推
flag = ip1.getIp2() - ip2.getIp2();
} else if (flag == 0) {
flag = ip1.getIp3() - ip2.getIp3();
} else if (flag == 0) {
flag = ip1.getIp4() - ip2.getIp4();
}
return flag;
}
}
public class Test {
public static void main(String[] args) {
ListIp ipList = new ArrayListIp();//定义ip List
ipList.add(new Ip(255, 2, 3, 4));//存放ip,你的ip是从文本中读出来的.我相信你会读,也会放.
ipList.add(new Ip(172, 7, 8, 2));
ipList.add(new Ip(2, 3, 4, 5));
ipList.add(new Ip(2, 1, 2, 3));
ipList.add(new Ip(1, 2, 3, 4));
ipList.add(new Ip(202, 2, 3, 4));
ComparatorUser comparator = new ComparatorUser();
Collections.sort(ipList, comparator);
for (Ip ip : ipList) {//输出结果,jdk 1.5以上可以这么遍历list,如果你是1.4 就用list的长度遍历即可
System.out.println(ip.getIp1() + "." + ip.getIp2() + "."
+ ip.getIp3() + "." + ip.getIp4());
}
}
}
运行结果如下.
1.2.3.4
2.1.2.3
2.3.4.5
172.7.8.2
202.2.3.4
255.2.3.4
java获取局域网ip地址 具体代码import java.io.*;
import java.util.*;
【java代码定义ip java代码定义json数据】import java.net.InetAddress;
public class Ip{
static public HashMap ping; //ping 后的结果集
public HashMap getPing(){ //用来得到ping后的结果集
return ping;
}
//当前线程的数量, 防止过多线程摧毁电脑
static int threadCount = 0;
public Ip() {
ping = new HashMap();
}
publicvoid Ping(String ip) throws Exception{
//最多30个线程
while(threadCount30)
Thread.sleep(50);
threadCount +=1;
PingIp p = new PingIp(ip);
p.start();
}
public void PingAll() throws Exception{
//首先得到本机的IP,得到网段
InetAddress host = InetAddress.getLocalHost();
String hostAddress = host.getHostAddress();
int k=0;
k=hostAddress.lastIndexOf(".");
String ss = hostAddress.substring(0,k+1);
for(int i=1;i =255;i++){//对所有局域网Ip
String iip=ss+i;
Ping(iip);
}
//等着所有Ping结束
while(threadCount0)
Thread.sleep(50);
}
public static void main(String[] args) throws Exception{
Ip ip= new Ip();
ip.PingAll();
java.util.Set entries = ping.entrySet();
Iterator iter=entries.iterator();
String k;
while(iter.hasNext()){
Map.Entry entry=(Map.Entry)iter.next();
String key=(String)entry.getKey();
String value=https://www.04ip.com/post/(String)entry.getValue();
if(value.equals("true"))

推荐阅读