รับตำแหน่งขององค์ประกอบอาร์เรย์โดยใช้วิธี BinarySearch
ตั้งค่าอาร์เรย์สตริง −
string[] str = { "a", "m", "i", "t"}; ตอนนี้รับตำแหน่งของอักขระ 't' โดยใช้ Array.BinarySearch -
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