JUST DO IT ~

          我只想當(dāng)個程序員

          C# ArrayList.BinarySearch 小問題 --- 必須是按順序存儲 才可以這樣 查找

          ArrayList.BinarySearch (Object) 使用默認(rèn)的比較器在整個已排序的 ArrayList 中搜索元素,并返回該元素從零開始的索引。

          ArrayList.BinarySearch (Object, IComparer) 使用指定的比較器在整個已排序的 ArrayList 中搜索元素,并返回該元素從零開始的索引。
          ArrayList.BinarySearch (Int32, Int32, Object, IComparer) 使用指定的比較器在已排序 ArrayList 的某個元素范圍中搜索元素,并返回該元素從零開始的索引。

          由 .NET Compact Framework 支持。



          ---強調(diào)一點 。因為是 2分查找。所以必須是 按照順序存放值, 否則出錯。

          有2個方式實現(xiàn) IComparer  和 類自己 實現(xiàn)一個接口 system 中的

          D:\c_\arraylist>Test
           person  CompareTo(object obj)  this person is 4 -- objthis person is 3
           person  CompareTo(object obj)  this person is 2 -- objthis person is 3
           person  CompareTo(object obj)  this person is 104 -- objthis person is 3
          find   person p3 = new person(3); -3   比較后得不到結(jié)果 嚴(yán)重問題
          -----
           person  CompareTo(object obj)  this person is 4 -- objthis person is 5
          find   person p3 = new person(3); 6
          -----
           person  CompareTo(object obj)  this person is 4 -- objthis person is 1
           person  CompareTo(object obj)  this person is 2 -- objthis person is 1
            find  person p1 = new person(1);  0
          -----
           person  CompareTo(object obj)  this person is 4 -- objthis person is 104
           person  CompareTo(object obj)  this person is 5 -- objthis person is 104
           person  CompareTo(object obj)  this person is 1 -- objthis person is 104
           person  CompareTo(object obj)  this person is -88 -- objthis person is 104
            find      person p6 = new person(104); -10  -- 比較后得不到結(jié)果 嚴(yán)重問題
          0 this person is 1
          1 this person is 2
          2 this person is 104
          3 this person is 3
          4 this person is 4
          5 this person is 122
          6 this person is 5
          7 this person is 1
          8 this person is -88




          using System;
          using System.Collections; 
          using System.Collections.Generic;

          public class Test
          {

              
          public class person : IComparable
              
          {
                  
          public int age = 0;
                  
          public person(int i)
                  
          {
                      
          this.age = i;

                  }

                  
          public override string ToString()
                  
          {

                      
          return "this person is " + age;

                  }


                  
          public int CompareTo(object obj)
                  
          {

                      Console.WriteLine(
          " person  CompareTo(object obj)  " + this.ToString() + " -- obj" + obj.ToString());

                      
          if (obj is person)
                      
          {
                          person temp 
          = (person)obj;

                          
          return  age.CompareTo(temp.age);
                      }


                      
          throw new ArgumentException("object is not a CompareTo ");
                  }




              }


              
          public static void Main(string[] args)
              
          {
                 



                  ArrayList list 
          = new ArrayList(300);

                  
                  person p1 
          = new person(1);
                  person p2 
          = new person(2);
                  person p3 
          = new person(3);
                  person p4 
          = new person(4);
                  person p5 
          = new person(5);
                  person p0 
          = new person(-88);


                  person p6 
          = new person(104);
                  person p7 
          = new person(122);


                
                  list.Add(p1);
                  list.Add(p2);
                  list.Add(p6);

                  list.Add(p3);
                  list.Add(p4);
                  list.Add(p7);

                  list.Add(p5);
                 list.Add(p1);
                 list.Add(p0);


                  
          /*

                 Console.WriteLine(list[1]);
                 list.Remove(1); //  1  will be object  for method input paramt 
                 Console.WriteLine(list[1]);
                 list.RemoveAt(1);
                 Console.WriteLine(list[1]);
                  
          */





                 
          /*
                  * 
                  *  ArrayList list0 = new ArrayList(2);
                          
                 list0.Add(new person(12));

                  * list.AddRange(list0);
                 Console.WriteLine(" 合并集合 AddRange  新的集合都在最后 " + list[2]);
                 Console.WriteLine(" 老集合的包含的數(shù)量    --添加新的以用不修改老的集合 " + list0.Count);
                 
          */




                 Console.WriteLine(
          "find   person p3 = new person(3); " + list.BinarySearch(p3));

                 Console.WriteLine(
          "-----");

                 Console.WriteLine(
          "find   person p3 = new person(3); " + list.BinarySearch(p5));

                 Console.WriteLine(
          "-----");


                 Console.WriteLine(
          "  find  person p1 = new person(1);  " + list.BinarySearch(p1));

                 Console.WriteLine(
          "-----");


                 Console.WriteLine(
          "  find      person p6 = new person(104); " + list.BinarySearch(p6));



                  
          for (int i = 0; i < list.Count; i++ )
                  
          {
                      Console.WriteLine(i 
          + " " + list[i].ToString() );


                  }






              }




          }



          /*

          Summary:
          Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.

          Parameters:
          x: The first object to compare.
          y: The second object to compare.

          Return Values:
          Value Condition Less than zero x is less than y. Zero x equals y. Greater than zero x is greater than y.

          Exceptions:
          System.ArgumentException: Neither x nor y implements the System.IComparable interface.-or- x and y are of different types and neither one can handle comparisons with the other.

          */

          class personIComparer : System.Collections.IComparer
          {

               
          public  int Compare(object x, object y)   {

                   Test.person p1 
          = x as Test.person;
                   Test.person p2 
          = y as Test.person; 

                  
          if ( p1 == null ) {
                     
          //??
                
                  }

                 
                   
          if (p1.age ==  p2.age ){
                       
          return 0
                   }
          else{
                       
          if (p1.age >  p2.age ) {

                           
          return 1;
                       }
          else{
                           
          return -1;
                       
                       }

                   }
           


                   
              }

           
          }






          /*








          public class Temperature : IComparable {
              /// <summary>
              /// IComparable.CompareTo implementation.
              /// </summary>
              public int CompareTo(object obj) {
                  if(obj is Temperature) {
                      Temperature temp = (Temperature) obj;

                      return m_value.CompareTo(temp.m_value);
                  }
                  
                  throw new ArgumentException("object is not a Temperature");    
              }

              // The value holder
              protected int m_value;

              public int Value {
                  get {
                      return m_value;
                  }
                  set {
                      m_value = value;
                  }
              }

              public int Celsius {
                  get {
                      return (m_value-32)/2;
                  }
                  set {
                      m_value = value*2+32;
                  }
              }
          }




          */




          posted on 2008-02-25 21:08 小高 閱讀(1643) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          <2008年2月>
          272829303112
          3456789
          10111213141516
          17181920212223
          2425262728291
          2345678

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類(352)

          收藏夾(19)

          關(guān)注的blog

          手冊

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 商都县| 安泽县| 台南市| 昂仁县| 平遥县| 柯坪县| 宁波市| 林周县| 富蕴县| 塘沽区| 漠河县| 绥滨县| 北碚区| 木里| 曲阳县| 望都县| 科尔| 吐鲁番市| 乐平市| 新乐市| 互助| 永丰县| 临猗县| 连南| 剑川县| 巴东县| 普兰店市| 福建省| 永德县| 三都| 浮梁县| 盐边县| 兰坪| 姜堰市| 荣成市| 灌阳县| 楚雄市| 潍坊市| 进贤县| 临武县| 金阳县|