ArrayList BinarySearch

Kev*_*vin 10 c# collections arraylist

我正忙着准备MCTS 70-536考试,根据考试书(Microsoft Press - .NET Framework - 应用程序开发基础自学训练套件第2版),此代码示例:

ArrayList al = new ArrayList();
al.AddRange(new string[] { "Hello", "world", "this", "is", "a", "test" });
Console.WriteLine(al.BinarySearch("this"));
Run Code Online (Sandbox Code Playgroud)

将值'2'输出到控制台,因为项'this'位于索引2.同意这是我运行该代码时得到的输出.

但是,如果我跑

Console.WriteLine(al.BinarySearch("world"));
Run Code Online (Sandbox Code Playgroud)

我希望在控制台中获得值1,因为'world'将在索引1,但是我得到值-7?

谁能解释一下这是如何工作的?

谢谢

GvS*_*GvS 11

您正在执行二进制搜索的数组未排序.这是BinarySearch运行的要求.

没有排序,混淆搜索算法,并使它认为"世界"应该在第7位,但它不在数组中:结果是-7.