package Array;class LowArray {private long[] a;private int nElem;public LowArray(int size) {this.a = new long[size];this.nElem = 0;}//更新代码public boolean setElem(int index, long value) {if(index >= nElem) return false;this.a[index] = value;return true;}//新增代码public int addElem(long value) {int i=0;for(;i<this.nElem;i++) {if(this.a[i]>value) {break;}}for(int k=this.nElem;k>i;k--) {this.a[k] = this.a[k-1];}this.a[i] = value;this.nElem++;return this.nElem;}//获取长度public int getLength() {return this.nElem;}//打印字符串public String toString() {String elemString = "";for(int i=0;i<this.nElem;i++) {elemString += this.a[i] + " ";}return elemString;}public boolean findKey(long value) {int i=0;for(; i<this.nElem; i++) {if(this.a[i] == value) break;}return (i==this.nElem) ? false : true;}//二分法查看数据public int findKeyByEr(long key) {int lowerBound = 0; //最小的值int upperBound = this.nElem -1; //最大的值int curIn;while(true) {curIn = (lowerBound + upperBound)/2;if(key == this.a[curIn]) {return curIn;}else if(lowerBound < upperBound) {return this.nElem;}else {if(key < this.a[curIn]) {upperBound = curIn-1;}else {lowerBound = curIn+1;}}}}/*** 基于递归二分法查找数据* @param key* @param lowerBound* @param upperBound* @return*/public int recFind(long key,int lowerBound,int upperBound) {int curIn = (lowerBound+upperBound)/2;if(this.a[curIn] == key) {return curIn;}else if(lowerBound > upperBound){return this.nElem;}else {if(this.a[curIn] > key) {return recFind(key,curIn+1,upperBound);}else {return recFind(key,lowerBound,curIn-1);}}}//删除一个keypublic boolean delKey(long value) {if(this.findKey(value)) {int i=0;for(; i<this.nElem; i++) {if(this.a[i] == value) break;}for(int t=i; t<this.nElem; t++) {this.a[t] = this.a[t+1];}this.nElem--;return true;}else {return false;}}//获取一个值public long getElem(int index) {return this.a[index];}}public class LowArrayApp{public static void main(String[] args) {LowArray arr;arr = new LowArray(100);int nElem = 0;int j = 0;long searchKey = 33;arr.addElem(77);arr.addElem(99);arr.addElem(44);arr.addElem(55);arr.addElem(22);arr.addElem(88);arr.addElem(11);arr.addElem(00);arr.addElem(66);arr.addElem(33);System.out.println(arr.toString());if(arr.findKey(searchKey)) {System.out.println("找到");}else {System.out.println("没找到");}System.out.println(searchKey);if(arr.delKey(searchKey)) {System.out.println("删除成功");}else {System.out.println("失败");}System.out.println(arr.toString());}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。