public class FindTest1 {private int[] a = null;private int len ;public FindTest1(int max){a = new int[max];len = 0;}public int find(int searchNum){int midIndex = -1;int lowIndex = 0;int highIndex = len-1;while(true){midIndex = (highIndex + lowIndex)/2;if(a[midIndex] == searchNum){return midIndex;}else if(searchNum < a[midIndex]){highIndex = midIndex -1;}else if(searchNum > a[midIndex]){lowIndex = midIndex + 1;}if(lowIndex > highIndex){return len;}}}public void insert(int target){int i;for(i = 0; i < len; i++){if(target < a[i]){break;}}for(int k = len; k > i; k--){a[k] = a[k-1];}a[i] = target;len++;}public void delete(int value){int status = find(value);if(status == len){return;}for(int k = status; k < len-1; k++){a[k] = a[k+1];}len--;}public void print(){for(int i = 0; i < len; i++){System.out.print(a[i]+"; ");}}public static void main(String[] args) {FindTest1 t = new FindTest1(10);t.insert(6);t.insert(2);t.insert(9);t.print();t.insert(5);System.out.println();t.print();System.out.println();t.delete(6);t.print();}}
java如何自定义算法,java算法--自定义数组
最新推荐文章于 2022-03-26 15:47:29 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)