Java Arrays 二分查找 对象 索引


Java Arrays 二分查找 对象 索引

package com.codingdict;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing unsorted array
      Object arr[] = {10,2,22,69};

      // sorting array
      Arrays.sort(arr);

      // let us print all the elements available
      System.out.println("The sorted array is:");
      for (Object number : arr) {
         System.out.println("Number = " + number);
      }

      // entering the value to be searched
      int searchVal = 22;

      // entering range of index
      int retVal = Arrays.binarySearch(arr,2,4,searchVal);

      System.out.println("The index of element 22 is : " + retVal);
   }
}