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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨庆雷 中级黑马   /  2014-8-14 23:43  /  4791 人查看  /  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 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 加入黑马