Java Arrays binarySearch() 方法



描述

Java Arrays binarySearch(Object[] a, Object key) 方法使用二分查詢演算法在指定的 Object 陣列中搜索指定的值。在呼叫此方法之前,陣列必須根據物件的自然順序進行排序。如果未排序,則結果未定義。

宣告

以下是 java.util.Arrays.binarySearch(Object[] a, Object key) 方法的宣告

public static int binarySearch(Object[] a, Object key)

引數

  • a − 要搜尋的陣列。

  • key − 要搜尋的值。

返回值

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

異常

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

Java Arrays binarySearch(Object[] a, int fromIndex, int toIndex, Object key) 方法

描述

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

宣告

以下是 java.util.Arrays.binarySearch(Object[] a, int fromIndex, int toIndex, Object key) 方法的宣告

public static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)

引數

  • a − 要搜尋的陣列。

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

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

  • key − 要搜尋的值。

返回值

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

異常

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

  • IllegalArgumentException − 如果 fromIndex > toIndex

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

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

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

package com.tutorialspoint;
import java.util.Arrays;
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")};

      // sorting array
      Arrays.sort(studentsArr);

      // 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);
      System.out.println("The index of student Julie is : " + retVal);
   }
}
class Student implements Comparable<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);
   }
   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

輸出

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

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

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

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

package com.tutorialspoint;
import java.util.Arrays;
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")};

      // sorting array
      Arrays.sort(studentsArr);

      // 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 object to be searched
      Student searchVal = new Student(1, "Julie");

      // entering the range of index
      int retVal = Arrays.binarySearch(studentsArr,0,1,searchVal);
      System.out.println("The index of student Julie is : " + retVal);
   }
}
class Student implements Comparable<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);
   }
   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

輸出

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

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

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

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

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

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

      // sorting array
      Arrays.sort(studentsArr);

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

      // entering the value to be searched
      Student searchVal = new Student(4, "Jene");
      int retVal = Arrays.binarySearch(studentsArr,searchVal);
      System.out.println("The index of student Jene is : " + retVal);
   }
}
class Student implements Comparable<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);
   }
   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

輸出

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

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
廣告