我會走向何方

          我又該走向何方

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            15 Posts :: 2 Stories :: 17 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          StudentDTO.java
          package?com.fangq.collections;
          /**
          ?*?
          ?*?
          @author?fangq
          ?*
          ?
          */

          public?class?StudentDTO?{
          ????
          private?String?xm;//姓名
          ????private?String?xh;//學號
          ????private?String?nl;//年齡
          ????public?String?getNl()?{
          ????????
          return?nl;
          ????}

          ????
          public?void?setNl(String?nl)?{
          ????????
          this.nl?=?nl;
          ????}

          ????
          public?String?getXh()?{
          ????????
          return?xh;
          ????}

          ????
          public?void?setXh(String?xh)?{
          ????????
          this.xh?=?xh;
          ????}

          ????
          public?String?getXm()?{
          ????????
          return?xm;
          ????}

          ????
          public?void?setXm(String?xm)?{
          ????????
          this.xm?=?xm;
          ????}

          ????
          }

          StudentComparator.java
          package?com.fangq.collections;

          import?java.util.Comparator;
          /**
          ?*?按dto的某個字段排序
          ?*?
          @author?fangq
          ?*
          ?
          */

          public?class?StudentComparator?implements?Comparator{

          ????
          public?int?compare(Object?arg0,?Object?arg1)?{
          ????????
          //?TODO?Auto-generated?method?stub
          ????????if(arg0?instanceof?StudentDTO&&arg1?instanceof?StudentDTO){
          ????????????StudentDTO?dto1?
          =?(StudentDTO)arg0;
          ????????????StudentDTO?dto2?
          =??(StudentDTO)arg1;
          ????????????
          int?nl1?=?Integer.parseInt(dto1.getNl());
          ????????????
          int?nl2?=?Integer.parseInt(dto2.getNl());
          ????????????
          if(nl1<nl2)
          ????????????????
          return?-1;
          ????????????
          else?if(nl1==nl2)
          ????????????????
          return?0;
          ????????????
          else
          ????????????????
          return?1;
          ????????????
          ????????}

          ????????
          return?-1;
          ????}


          ????

          }

          TestComparator.java
          package com.fangq.collections;

          import java.util.Arrays;

          import org.apache.commons.lang.math.RandomUtils;
          /**
          ?*
          ?* @author?fangq
          ?*/
          public class TestComparator {
          ?public static void main(String[] args){
          ??StudentDTO[] dtos = new StudentDTO[10];
          ??for(int i=0;i<dtos.length;i++){
          ???dtos[i] = new StudentDTO();
          ???dtos[i].setXh("xh"+i);
          ???dtos[i].setXm("xm"+i);
          ???dtos[i].setNl(String.valueOf(i+RandomUtils.nextInt(20)));
          ??}
          ??System.out.println("排序前===============");
          ??for(int j=0;j<dtos.length;j++)
          ??System.out.println("姓名:"+dtos[j].getXm()+"年齡:"+dtos[j].getNl());
          ??
          ??Arrays.sort(dtos,new StudentComparator());
          ??System.out.println("排序后===============");
          ??for(int k=0;k<dtos.length;k++)
          ??System.out.println("姓名:"+dtos[k].getXm()+"年齡:"+dtos[k].getNl());
          ??
          ?}
          }

          posted on 2006-08-26 16:10 hama 閱讀(1430) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 新绛县| 桓台县| 全椒县| 胶州市| 赤峰市| 合阳县| 青河县| 德惠市| 双鸭山市| 全州县| 大荔县| 吉木乃县| 东至县| 孟津县| 莱西市| 绥化市| 安庆市| 北川| 资阳市| 潜江市| 贵州省| 绥宁县| 云和县| 中西区| 融水| 龙川县| 乌拉特前旗| 金山区| 思茅市| 安图县| 宜川县| 兰西县| 株洲县| 托克逊县| 如东县| 东山县| 西峡县| 筠连县| 礼泉县| 渝北区| 广水市|