<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JUST DO IT ~

    我只想當個程序員

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

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

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

    由 .NET Compact Framework 支持。



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

    有2個方式實現 IComparer  和 類自己 實現一個接口 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   比較后得不到結果 嚴重問題
    -----
     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  -- 比較后得不到結果 嚴重問題
    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(" 老集合的包含的數量    --添加新的以用不修改老的集合 " + 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 小高 閱讀(1640) 評論(0)  編輯  收藏


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


    網站導航:
     

    導航

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

    統計

    常用鏈接

    留言簿(3)

    隨筆分類(352)

    收藏夾(19)

    關注的blog

    手冊

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 最近中文字幕国语免费完整| 国产V片在线播放免费无码| 99免费在线观看视频| 国产亚洲一区二区精品| 本免费AV无码专区一区| 亚洲色大成网站www永久一区| 一级成人生活片免费看| 亚洲日韩涩涩成人午夜私人影院| 免费无码又爽又黄又刺激网站| 免费播放春色aⅴ视频| 美女被免费视频网站a| 精品国产香蕉伊思人在线在线亚洲一区二区 | 成人爽a毛片免费| 亚洲一区二区女搞男| 爱丫爱丫影院在线观看免费| 亚洲成在人线av| 18禁成人网站免费观看| 亚洲乱码一二三四区国产| 成年男女男精品免费视频网站| 亚洲欧美日韩中文二区| 亚洲第一网站男人都懂| 两个人看的www高清免费观看| 亚洲五月激情综合图片区| 亚洲成在人线aⅴ免费毛片| 国产精品亚洲专区无码WEB| 亚洲国产成人爱av在线播放| 91在线免费视频| 亚洲欧洲自拍拍偷综合| 国产精品免费视频播放器| 国产一级a毛一级a看免费视频| 亚洲一区二区三区夜色| 久久久久久免费视频| 国产亚洲一卡2卡3卡4卡新区| 亚洲精品tv久久久久| 在线观看免费av网站| 亚洲AV无码成人精品区狼人影院| 亚洲一级黄色视频| 114级毛片免费观看| 国产亚洲精品91| 亚洲一区二区三区高清| 免费大学生国产在线观看p|