java数组测试类中增查删改_java学习之—数组的曾删改查

/**

* 数组的曾删改查

* 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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值