黑马程序员技术交流社区

标题: 第5题 [打印本页]

作者: doglovely    时间: 2016-3-13 14:22
标题: 第5题
public class Test5 {

        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                ipSort();
        }
        //定义一个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);
                }
        }






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2