数组声明方式
1.int [] aa;
2.int aa [];不推荐
数组实例化方式(必须要声明数组大小)
- int [] aa=new int[555];//默认555
- int [] aa=new int[]{1,2,3,4,5,6,7,8,9};
- int [] aa={1,2,3,4,5,6,7,8,9};
一、前置条件一条有顺序的数组
二、查找代码
public class Main {
public static void main(String[] args) {
int[] a = {10, 15, 20, 31, 54, 75, 96, 150,188, 999};
int low = 0;
int high = a.length - 1;
int search = 188;
int mid;
int num=0;
while (low < high) {
num++;
mid = low +(high-low)/ 2;
if (a[mid] == search) {
System.out.println("在第"+num+"次找到了,找到的下标为:"+mid);
return;
}
if (search < a[mid]) {
high = mid;
}
if (a[mid] < search) {
low = mid;
}
}
}
}
结果
在第4次找到了,找到的下标为:8
|