A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨庆雷 中级黑马   /  2014-8-14 23:43  /  4741 人查看  /  13 人回复  /   3 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. * 6、 把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。
  3. *
  4. *        61.54.231.245
  5. *        61.54.231.9
  6. *        61.54.231.246
  7. *        61.54.231.48
  8. *        61.53.231.249
  9. *
  10. * */
  11. public class Test6 {
  12.                
  13.         public static void main(String[] args){
  14.                 //存放IP地址的相对路径
  15.                 String str = ".\\src\\com\\itheima\\IP.txt";
  16.                 //把IP地址以字符串的形式作为参数
  17.                 List<String> list = printIP(str);
  18.                 //把返回的ArrayLst的对象  list用自定义的比较器Mycomparator排序
  19.                 Collections.sort(list,new Mycomparator());
  20.                 //用迭代器把比较后的结果打印到控制台
  21.                 Iterator<String> orderit = list.iterator();
  22.                 while (orderit.hasNext()) {
  23.                         System.out.println(orderit.next());
  24.                 }
  25.         }

  26.         private static List<String> printIP(String str) {
  27.                 //因为是纯文本文档此处可以用字符流,创建字符流缓冲对象,提高效率
  28.                 BufferedReader bufr = null;
  29.                 //创建ArrayList对象,用于存储Ip地址
  30.                 List<String> list = new ArrayList<String>();
  31.         try {
  32.                 //创建字符读取流加入缓存
  33.                 bufr=new BufferedReader(new FileReader(str));
  34.                 //读入的一行字符
  35.                 String line=null;
  36.                 while((line=bufr.readLine())!=null){
  37.                         //每读取一行就存入list集合中
  38.                         list.add(line);
  39.                 }
  40.                 
  41.                 } catch (IOException e) {
  42.                         // TODO Auto-generated catch block
  43.                         e.printStackTrace();
  44.                 }
  45.         //字符串数组
  46.       
  47.         try {
  48.                 if(bufr != null){
  49.                         bufr.close();
  50.                 }
  51.                 } catch (IOException e) {
  52.                         // TODO Auto-generated catch block
  53.                         e.printStackTrace();
  54.                 }
  55.                 return list;
  56.         }
  57.        
  58.        
  59. }

  60. class Mycomparator implements Comparator<String>{

  61.         @Override
  62.         public int compare(String s1, String s2) {
  63.                 //创建两个数组,用于存储切割后的IP
  64.                 String[] arr1 = s1.split("\\.");
  65.                 String[] arr2 = s2.split("\\.");
  66.                 //定义两个用于存储IP地址计算后的Long值对象,因为数据比较大,超出了int的范围
  67.                 long num1 = 0;
  68.                 long num2 = 0;
  69.                
  70.                 for (int i = 0; i < arr1.length; i++) {
  71.                         //因为每一段IP的最大值是256,每一段乘以256的3-i的次方,方便下面的比较
  72.                         num1 = (long) (num1 + Integer.parseInt(arr1[i])*Math.pow(256,(3-i)));
  73.                 }
  74.                 for (int i = 0; i < arr2.length; i++) {
  75.                         //因为每一段IP的最大值是256,每一段乘以256的3-i的次方,方便下面的比较
  76.                         num2 = (long) (num2 + Integer.parseInt(arr2[i])*Math.pow(256,(3-i)));
  77.                 }
  78.                 //因为 num1-num2 的运算结果可能会超出int的范围,所以不能直接返回 num1-num2,最好先判断再返回1,-1或者0
  79.                 if(num1 - num2 > 0 ){
  80.                         return 1;
  81.                 }else if(num1 - num2 < 0 ){
  82.                         return -1;
  83.                 }
  84.                 return 0;
  85.         }
  86.        
  87. }
复制代码


13 个回复

倒序浏览
很好,赞一个
回复 使用道具 举报
学习 了
回复 使用道具 举报
楼主大才,赞!
回复 使用道具 举报
/*
* 第8题: 把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。
        61.54.231.245
        61.54.231.9
        61.54.231.246
        61.54.231.48
        61.53.231.249
       
分析:本题涉及IO流读取文本数据,正则表达式,字符串排序的相关知识       
     1、定义一个字符读取流从指定的txt中读取IP数据
     2、将读取到的IP数据转换为字符串,然后进行排序
        IP地址分为4段,每一段的最大位数是3,先将最低位补齐(在前面补0),补完后由于有的地址段已经超过3位,
                     再进行处理,将前面多余的0去掉,保留3位。此时得到的IP每一段都是3位的,将得到的IP地址排序
                排序方法可将字符串转换成数组,利用工具类Arrays.sort排序
                也可以将IP地址存入到TreeSet中排序
*/
public class Test8 {
        public static void main(String[] args) {
                //从txt文件中读取IP地址
                String str = getIp();
                //对ip地址进行排序
                Mysort(str);
        }
       
    public static void Mysort(String str) {
            //第一次替换,给所有的地址段前补两个0,
                str = str.replaceAll("(\\d+)", "00$1");
                //第二次替换,去除地址段前多余的0,让每一段都是3位
                str = str.replaceAll("0*(\\d{3})", "$1");
                //读取的IP地址字符串中带有换行符,就以换行符来切割字符串,
                //windows中的换行符为\r\n,正则表达式中\都以成对的形式出现,所以切割规则为\\r\\n
                String[] arr = str.split("\\r\\n");
               
                //排序方法一
                //调用数组工具类的sort方法对数组排序
                Arrays.sort(arr);
                //遍历数组,打印排序后的IP地址
                for (int i = 0; i < arr.length; i++) {
                        //第三次替换,排序后的IP地址段中有的前面有多余无效的0,将其去掉
                        System.out.println(arr[i].replaceAll("0+(\\d+)", "$1"));
                }
                //定义分割付区分两种不同排序方式打印的结果
                System.out.println("----------------------------");
               
                //排序方法二
                //通过TreeSet集合进行排序
                TreeSet<String > set = new TreeSet<String>();
                //迭代取出切割后的IP地址字符串将其添加到TreeSet集合中
                for (String ip : arr) {
                        set.add(ip);
                }
                //遍历set集合,打印排序后的IP地址
                for (String s1 : set) {
                        //第三次替换,排序后的IP地址段中有的前面有多余无效的0,将其去掉
                        System.out.println(s1.replaceAll("0+(\\d+)", "$1"));
                }
               
        }

    public static String  getIp() {
        // 定义一个字符读取流,读取txt中的IP
                BufferedReader bfr = null;
            //定义一个字符串用来存储读取到IP字符串数据
                String line = null;
                try {
                           bfr = new BufferedReader(new FileReader("ip.txt"));
                           //定义字符缓冲数组
                           char[] buf = new char[1024];
                           int len = 0;
                           while((len = bfr.read(buf))!=-1){
                                   //将读取到的字符数据赋给line
                                   line = new String(buf, 0, len);
                           }
                } catch (Exception e) {
           e.printStackTrace();
                }finally{
                         //判断流是否为空,若不为空则关闭字符读取流
                        if (bfr != null) {
                                try {
                                         //调用close方法关闭流对象
                                        bfr.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
                //返回IP字符串数据
                return line;
        }
}

回复 使用道具 举报 2 0
学习了,很受用
回复 使用道具 举报
赞一个!
回复 使用道具 举报
谢谢,参考下
回复 使用道具 举报
cczheng 发表于 2014-11-30 20:57
/*
* 第8题: 把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。
        61.54.2 ...

大神,谢谢分享.
回复 使用道具 举报
好  谢谢                                   
回复 使用道具 举报
谢谢学习了
回复 使用道具 举报
弗人 中级黑马 2015-10-14 12:00:48
12#
这样子真的好么?
回复 使用道具 举报
天呐,太恐怖了。找了半天才找到这题的答案。
回复 使用道具 举报
赞赞赞,谢谢楼主
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马