Java Arrays binarySearch() 方法



描述

Java Arrays binarySearch(T[] a, T key, Comparator<? super T> c) 方法使用二分查詢演算法在指定的 Object 陣列中搜索指定的值。在呼叫此方法之前,必須根據指定的 Comparator 對陣列進行排序。如果未排序,則結果未定義。

宣告

以下是 java.util.Arrays.binarySearch(T[] a, T key, Comparator<? super T> c) 方法的宣告

public static <T> int binarySearch​(T[] a, T key, Comparator<? super T> c)

引數

  • a − 要搜尋的陣列。

  • key − 要搜尋的值。

  • c − 陣列排序使用的比較器。空值表示應使用元素的自然順序。

返回值

如果搜尋鍵包含在陣列中,則此方法返回搜尋鍵的索引;否則,返回 (-(插入點) - 1)。插入點是鍵將被插入到陣列中的位置:大於鍵的第一個元素的索引,如果陣列中的所有元素都小於指定的鍵,則為 a.length。

異常

  • ClassCastException − 如果陣列包含使用指定的比較器不能相互比較的元素,或者使用此比較器搜尋鍵不能與陣列的元素進行比較。

Java Arrays binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) 方法

描述

Java Arrays binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) 方法使用二分查詢演算法在一個指定的 Object 陣列的範圍內搜尋指定的值。在呼叫此方法之前,該範圍必須根據指定的 Comparator 進行排序。如果未排序,則結果未定義。

宣告

以下是 java.util.Arrays.binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) 方法的宣告

public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)

引數

  • a − 要搜尋的陣列。

  • fromIndex − 要搜尋的第一個元素的索引(包含)。

  • toIndex − 要搜尋的最後一個元素的索引(不包含)。

  • key − 要搜尋的值。

  • c − 陣列排序使用的比較器。空值表示應使用元素的自然順序。

返回值

此方法返回搜尋鍵的索引,如果它包含在陣列中,否則返回 (-(插入點) - 1)。插入點是鍵將被插入到陣列中的位置;該範圍內大於鍵的第一個元素的索引,如果該範圍內的所有元素都小於指定的鍵,則為 toIndex。

異常

  • ClassCastException − 如果搜尋鍵與陣列的元素不可比較。

  • IllegalArgumentException − 如果 fromIndex > toIndex

  • ArrayIndexOutOfBoundsException − 如果 fromIndex < 0 或 toIndex > a.length

  • ClassCastException − 如果陣列包含使用指定的比較器不能相互比較的元素,或者使用此比較器搜尋鍵不能與陣列的元素進行比較。

在物件陣列上執行二分查詢示例

以下示例演示了 Java Arrays binarySearch(T[], key, comparator) 方法的用法。首先,我們建立了一個 Student 物件陣列,對其進行排序並列印。然後對一個值執行二分查詢並列印結果。

package com.tutorialspoint;
import java.util.Arrays;
import java.util.Comparator;
public class ArrayDemo {
   public static void main(String[] args) {

      // initializing unsorted Object array
      Student studentsArr[] = new Student[]{new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert")};
      RollNoComparator comparator = new RollNoComparator();
      
      // sorting array
      Arrays.sort(studentsArr, comparator);

      // let us print all the elements available in list
      System.out.println("The sorted Object array is:");
      for (Student student : studentsArr) {
         System.out.println("Student = " + student);
      }

      // entering the student to be searched
      Student searchVal = new Student(1, "Julie");
      int retVal = Arrays.binarySearch(studentsArr,searchVal,comparator);
      System.out.println("The index of student Julie is : " + retVal);
   }
}
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
   public int getRollNo() {
      return rollNo;
   }
   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}
class RollNoComparator implements Comparator<Student>{
   @Override
   public int compare(Student o1, Student o2) {
      return o1.getRollNo()-o2.getRollNo();
   }	
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

The sorted Object array is:
Student = [ 1, Julie ]
Student = [ 2, Robert ]
Student = [ 3, Adam ]
The index of student Julie is : 0

在物件子陣列上執行二分查詢示例

以下示例演示了 Java Arrays binarySearch(T[], fromIndex, toIndex, key, comparator) 方法的用法。首先,我們建立了一個 Student 物件陣列,對其進行排序並列印。然後對子陣列上的一個值執行二分查詢並列印結果。

package com.tutorialspoint;
import java.util.Arrays;
import java.util.Comparator;
public class ArrayDemo {
   public static void main(String[] args) {

      // initializing unsorted Object array
      Student studentsArr[] = new Student[]{new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert")};
      RollNoComparator comparator = new RollNoComparator();
      
      // sorting array
      Arrays.sort(studentsArr, comparator);

      // let us print all the elements available in list
      System.out.println("The sorted Object array is:");
      for (Student student : studentsArr) {
         System.out.println("Student = " + student);
      }

      // entering the student to be searched
      Student searchVal = new Student(1, "Julie");
      int retVal = Arrays.binarySearch(studentsArr,0,1,searchVal,comparator);
      System.out.println("The index of student Julie is : " + retVal);
   }
}
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
   public int getRollNo() {
      return rollNo;
   }
   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}
class RollNoComparator implements Comparator<Student>{
   @Override
   public int compare(Student o1, Student o2) {
      return o1.getRollNo()-o2.getRollNo();
   }	
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

The sorted Object array is:
Student = [ 1, Julie ]
Student = [ 2, Robert ]
Student = [ 3, Adam ]
The index of student Julie is : 0

在物件陣列上對不存在的物件執行二分查詢示例

以下示例演示了 Java Arrays binarySearch(Object[], key, comparator) 方法的用法。首先,我們建立了一個 Student 物件陣列,對其進行排序並列印。然後對陣列中不存在的值執行二分查詢並列印結果。

package com.tutorialspoint;
import java.util.Arrays;
import java.util.Comparator;
public class ArrayDemo {
   public static void main(String[] args) {

      // initializing unsorted Object array
      Student studentsArr[] = new Student[]{new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert")};

      RollNoComparator comparator = new RollNoComparator();
      
      // sorting array
      Arrays.sort(studentsArr, comparator);

      // let us print all the elements available in list
      System.out.println("The sorted Object array is:");
      for (Student student : studentsArr) {
         System.out.println("Student = " + student);
      }

      // entering the student to be searched
      Student searchVal = new Student(4, "Jene");
      int retVal = Arrays.binarySearch(studentsArr,searchVal,comparator);
      System.out.println("The index of student Jene is : " + retVal);
   }
}
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
   public int getRollNo() {
      return rollNo;
   }
   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}
class RollNoComparator implements Comparator<Student>{
   @Override
   public int compare(Student o1, Student o2) {
      return o1.getRollNo()-o2.getRollNo();
   }	
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果:

The sorted Student array is:
Student = [ 1, Julie ]
Student = [ 2, Robert ]
Student = [ 3, Adam ]
The index of student Jene is : -4
java_util_arrays.htm
廣告