氟塑料離心泵www.buybeng.com

          jquery教程http://www.software8.co/wzjs/jquery/

          用二分法對引用類型進行查找

          首先,我們都清楚,如果想在一堆數據中進行查找的話,這些數據最好都是排好序的,這樣才方便查找。

                     也就是說查找,一般是建立在排序的基礎上,下面我們就拿上篇博文中的例子:

                 http://www.aygfsteel.com/jialisoft/archive/2012/10/05/389054.html

                    對Date這個引用類型進行查找

          1. public class TestSearchDate  
          2. {  
          3.     public static void main(String args[])  
          4.     {  
          5.         Date[] d=new Date[5];  
          6.         d[0]=new Date(2006,5,4);  
          7.         d[1]=new Date(2006,7,4);  
          8.         d[2]=new Date(2008,5,4);  
          9.         d[3]=new Date(2004,5,9);  
          10.         d[4]=new Date(2004,5,4);  
          11.           
          12.         bubbleSort(d);  
          13.           
          14.         for(int i=0;i<=d.length-1;i++)  
          15.         {  
          16.             System.out.println(d[i]);  
          17.         }  
          18.           
          19.         Date oneDate=new Date(2008,5,4);    //查找這個日期中排序序列的什么位置  
          20.         System.out.println(Search(oneDate,d));  
          21.     }  
          22.       
          23.     //用冒泡排序法,進行排序  
          24.     public static void bubbleSort(Date[] d)  
          25.     {  
          26.         for(int i=d.length-1;i>=1;i--)  
          27.         {  
          28.             for(int j=0;j<i;j++)  
          29.             {  
          30.                 if(d[j].Compare(d[j+1])>0)  
          31.                 {  
          32.                     Date temp;  
          33.                     temp=d[j];d[j]=d[j+1];d[j+1]=temp;  
          34.                 }  
          35.             }  
          36.         }  
          37.     }  
          38.       
          39.     //用二分法查找oneDate在d這個Date數組的什么位置  
          40.     public static int Search(Date oneDate,Date[] d)  
          41.     {  
          42.         Date temp;  
          43.         int current;  
          44.         int startPos=0;  
          45.         int endPos=d.length-1;  
          46.           
          47.         current=d.length/2;  
          48.           
          49.         while(startPos<=endPos)  
          50.         {  
          51.             if(oneDate.Compare(d[current])==0)  
          52.             {  
          53.                 return current; //找到了,返回當前的索引  
          54.             }  
          55.             else if(oneDate.Compare(d[current])>0)  
          56.             {  
          57.                 startPos=current+1;  
          58.             }  
          59.             else  
          60.             {  
          61.                 endPos=current-1;  
          62.             }  
          63.             current=(startPos+endPos)/2;  
          64.         }  
          65.         return -1;  //表示沒有找到  
          66.     }  
          67. }  
          68.   
          69. class Date  
          70. {  
          71.     int year,month,day;  
          72.     Date(int y,int m,int d)  
          73.     {  
          74.         year=y;month=m;day=d;  
          75.     }  
          76.       
          77.     //定義一個比較的方法  
          78.     public int Compare(Date date)  
          79.     {  
          80.         return year>date.year?1  
          81.                 :year<date.year?-1  
          82.                 :month>date.month?1  
          83.                 :month<date.month?-1  
          84.                 :day>date.day?1  
          85.                 :day<date.day?-1:0;  
          86.     }  
          87.     //重寫toString方法  
          88.     public String toString()  
          89.     {  
          90.         return "year-month-day:"+year+"-"+month+"-"+day;  
          91.     }  
          92. }  


                       主要看就是其中的二分法查找,我想大家一看就明白。

                  運行結果如下:

          posted on 2012-10-05 10:16 你爸是李剛 閱讀(878) 評論(0)  編輯  收藏


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


          網站導航:
           
          <2012年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          技術網站

          行業網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          站長網 氟塑料離心泵 注塑機 液晶廣告機
          主站蜘蛛池模板: 浦北县| 滁州市| 青冈县| 改则县| 台北市| 元谋县| 延安市| 华蓥市| 广平县| 万州区| 武夷山市| 加查县| 久治县| 长乐市| 高青县| 理塘县| 城固县| 揭阳市| 桂阳县| 青阳县| 扶沟县| 铜山县| 滦平县| 宝兴县| 广德县| 曲麻莱县| 临武县| 门源| 偃师市| 和田县| 浮梁县| 郁南县| 晋城| 石门县| 中宁县| 夏津县| 宜宾县| 平乡县| 抚宁县| 长岭县| 平湖市|