如下所示:public class Test{ public static void main(String[] args) { Long[] arr = new Long[100000]; for(int i =0;i&l
如下所示:
public class Test{ public static void main(String[] args) { Long[] arr = new Long[100000]; for(int i =0;i<100000;i++) { arr[i] = (long) i; } System.out.println(binarySearch(arr, 3L)); Comparable midVal = (Comparable) 2L;; System.out.println(midVal.compareTo(2l)); } private static int binarySearch(Long[] arr, long l) { return binarySearch0(arr,0,arr.length,l); } private static int binarySearch0(Object[] a, int fromIndex, int toIndex, Object key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; Comparable midVal = (Comparable) a[mid]; int cmp = midVal.compareTo(key); if (cmp < 0) low = mid + 1; else if (cmp > 0) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. }}
--结束END--
本文标题: Java JDK 二分法 分析demo(推荐)
本文链接: https://lsjlt.com/news/224745.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0