Arrays.copyOfRange(int[] arr,int開始下標(biāo),int結(jié)束下標(biāo))//左閉右開
System.arraycopy(int[] source,int開始下標(biāo),int[] dest,int開始下標(biāo),int拷貝多少個)
這里是手動拷貝:
public static int[] copyOf(int[] original, int newLength) {
int[] dest = new int[newLength];
int length = original.length <= newLength ? original.length : newLength;
for (int i = 0; i < length; i++) {
dest[i] = original[i];
}
return dest;
}
mid作為分水嶺, 不斷地縮小范圍
// 前提需要數(shù)組是有序的
public static int binarySearch(int[] a, int v) {
int left = 0;
int right = a.length;
while (left < right) {
int mid = (left + right) / 2;
if (v == a[mid]) {
return mid;
} else if (v < a[mid]) {
right = mid;
} else {
left = mid + 1;
}
}
return -1;
}
public static int binarySearch3(int[] a, int v) {
int left = 0;
int right = a.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (v == a[mid]) {
return mid;
} else if (v < a[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
文章名稱:數(shù)組拷貝,二分法查找
URL分享:
http://weahome.cn/article/jhjdie.html