C# 陣列的 BinarySearch 方法


使用 BinarySearch 方法獲取陣列元素的位置。

設定一個字串陣列 −

string[] str = { "a", "m", "i", "t"};

現在使用 Array.BinarySearch 獲取字元“t”的位置 −

Array.BinarySearch(str, "t");

以下是完整程式碼 −

示例

 線上演示

using System;
using System.Text;
public class Demo {
   public static void Main() {
      string[] str = { "a", "m", "i", "t"};
      // Using BinarySearch method to get location of character 't'
      int res = Array.BinarySearch(str, "t");
      // displaying the location
      Console.WriteLine("Index : "+res);
   }
}

輸出

Index : 3

更新日期:22-6 月-2020

68 次瀏覽

開啟你的事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.