ip排序的答案?
//定义一个ip排序的方法
public static void ipSort(){
String ip="61.54.231.245 61.54.231.9 61.54.231.246 61.53.231.249";
//ip最多需要补两个0
ip = ip.replaceAll("(\\d+)", "00$1");
//System.out.println(ip);
//ip去掉多余的0,ip变成都是三位数
ip = ip.replaceAll("0*(\\d{3})","$1");
//System.out.println(ip);
//对ip进行切割,变成字符数组
String[] arr = ip.split(" ");
//用TresSet进行知我排序
TreeSet<String> ts = new TreeSet<String>();
for(String s : arr){
ts.add(s);
}
for(String s: ts){
//把0去掉
s = s.replaceAll("0*(\\d+)", "$1");
System.out.println(s);
}
} |