/**
* 数组的曾删改查
* Create by Administrator
* 2018/6/8 0008
* 上午 9:54
**/
public class HighArray {
private long[] a;
private int nElems;
public HighArray(int max){
a = new long[max];
nElems = 0;
}
/**
* 使用二分查找法
* @param searchKey
* @return
*/
public int findOne(long searchKey){
// 数组的第一个
int lowerBound = 0;
// 数组的最后一个
int upperBound = nElems - 1;
// 数组的一半,下标
int curIn;
while (true){
curIn = (lowerBound + upperBound) / 2;
//判断这个数是不是数组的对半curIn是否相等
if(a[curIn] == searchKey){
return curIn;
}else if(lowerBound > upperBound){ //如果成立 测范围已经不存在了
return nElems;
}else {
//如果成立 则将范围设置curIn的后半部分,反之就设置为前半部分
if(a[curIn] < searchKey){
lowerBound = curIn + 1;
}else {
upperBound = curIn - 1;
}
}
}
}
public boolean find(long searchKey){
int j;
for(j = 0; j < nElems; j++){
if(a[j] == searchKey){
break;
}
}
if (j == nElems){
return false;
}else {
return true;
}
}
public void insert(long value){
a[nElems] = value;
nElems++;
}
/**
* 保存排序
* @param value
*/
public void save(long value){
int j;
for (j=0; j
if(a[j] > value){
break;
}
}
for (int i = nElems; i > j; i--) {
a[i] = a[i-1];
}
a[j] = value;
nElems++;
}
public boolean delete(long value){
int j = findOne(value);
// for(j = 0; j < nElems; j++){
// if(a[j] == value){
// break;
// }
// }
if (j == nElems){
return false;
}else {
for (int i = j; i < nElems; i++) {
a[i] = a[i+1];
}
nElems--;
return true;
}
}
public void show(){
for (int i = 0; i < nElems; i++) {
System.out.print(a[i]+ " ");
}
System.out.println("");
}
public static void main(String[] args) {
HighArray array = new HighArray(100);
array.save(10);
array.save(3);
array.save(2);
array.save(11);
array.save(9);
array.save(5);
array.save(4);
array.save(2);
array.save(7);
array.show();
long num = 0;
System.out.println("查找结果"+num+":"+ array.find(num));
array.delete(2);
array.delete(5);
array.delete(8);
array.show();
}