如何在ArrayList上使用BinarySearch方法 [英] How to use BinarySearch Method on ArrayList

查看:72
本文介绍了如何在ArrayList上使用BinarySearch方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#编程的新手.如果使用BinarySerach方法来搜索数组列表中的字符串,则以下程序给出的值不正确.有人可以解释这个程序是什么问题.

I am new to programming in C#. Following program gives incorrect value if BinarySerach method is used to serach a string in the array list. can somebody explain what is the problem in this program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CollectionDirectories
{
    class sortArrayList
    {
        static int sr_no;
        static void Main()
        {
            
            ArrayList al = new ArrayList();
            al.Add("Hello");
            al.Add("World");
            al.Add("this");
            al.Add("is");
            al.Add("yes");
            al.Add("a");
            al.Add("test");
            al.Remove("test");
            al.Insert(4,"not");
// comment on output: In this list index of "World" is 1 but out put is different.
            Console.WriteLine("First Search ="+al.BinarySearch("World"))
Console.WriteLine("_______________");
            al.Sort();
            foreach (object s in al)
                Console.WriteLine(s.ToString());f
// comment on output: If reverseSort is used the output after reverse is length of arrarylist +1
            al.Sort(new reverseSort());
            Console.WriteLine("after reverse____________");
            foreach (object s in al)
            {
                sr_no++;
                Console.WriteLine(sr_no + ":" + s.ToString());
            }
            Console.WriteLine("sr. of word_______________");
          
            Console.WriteLine("Second Search = "+ al.BinarySearch("World"));
            Console.WriteLine("____________");
            foreach (object s in al)
                Console.WriteLine(s.ToString());
          
            Console.Read();
        }
        static int i;
        public class reverseSort : IComparer
        {
            int IComparer.Compare(Object x, Object y)
            {
                i++;
                Console.WriteLine(i+&" "+ x + " "+ y);
                  
                return ((new CaseInsensitiveComparer()).Compare(y, x));
            }
        }
    }
}

推荐答案

您是否阅读过文档?


Did you read the documentation?


Rajiv Yelkawar写道:
Rajiv Yelkawar wrote:

//输出注释:在该列表中,世界"的索引为1,但输出结果不同.

// comment on output: In this list index of "World" is 1 but out put is different.


第一次搜索列表时,您根本还没有对列表进行排序,因此搜索当然会得到螺丝钉的结果.



The first time that you search the list you haven''t sorted it yet at all so of course the search is going to give screwey results.


Rajiv Yelkawar写道:
Rajiv Yelkawar wrote:

//输出注释:如果使用了reverseSort,则反向后的输出为arrarylist的长度

// comment on output: If reverseSort is used the output after reverse is length of arrarylist


第二次搜索是在对列表进行反向排序之后进行的,因此再次搜索将失败.

列表需要按增加的顺序进行排序,搜索才能起作用.


The second time you search you''re doing it after sorting the list backwards so again, the search will fail.

The list needs to be sorted with in INCREASING order for the search to work.


实际上,它可以为您提供文档所确切说明的内容(它将为您提供(修复编译错误后).问题在于BinarySearch方法期望将要排序的事物按升序排列,否则搜索将无法进行.您正在按降序对列表进行排序,然后进行搜索.请参考文档 [
Actually, it gives you exactly what the documentation tells you it will give you (after you fix the compile errors). The problem is that the BinarySearch method expects things to be sorted is ascending order or else the search won''t work. You''re sorting the list in descending order and then searching. Please refer to the documentation[^].


尊敬的Jimmanual,
感谢您的快速和适当的答复.如您在代码中看到的,我创建了一个实现IComparer的公共类reverseSort.如果我注释"al.Sort(new reverseSort());",程序将在第一次搜索时给出错误的输出.在代码中输入,并在第二次搜索时更正输出,反之亦然.我很遗憾没有在我的原始问题中提出这个问题.不过谢谢.
Dear Jimmanual,
Thanks for quick and appropreate reply. As you can see in the code I have created a public class reverseSort implementing IComparer. Program gives incorrect output at first search if I comment "al.Sort(new reverseSort());" in the code and correct output at second search and vice versa. I regreat for not putting this in my original question. Nevertheless thanks.


这篇关于如何在ArrayList上使用BinarySearch方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆