封装面向对象数组,并且支持有序和无序,查询元素分为顺序查找和二分法。
1 /** 2 * @ClassName: MyArray 3 * @Description: 封装自己数组 4 * @author dongye 5 * @date 2016年3月1日 上午9:28:40 6 * 7 */ 8 public class MyArray { 9 private long[] arr; 10 //有效长度 11 private int elements; 12 13 public MyArray(){ 14 arr=new long[50]; 15 } 16 17 public MyArray(int maxsize){ 18 arr=new long[maxsize]; 19 } 20 21 /** 22 * @Description:无序插入 23 * @return void 返回类型 24 * @author dongye 25 * @date 2016年3月1日 上午9:31:01 26 */ 27 public void insert(long value) { 28 arr[elements]=value; 29 elements++; 30 } 31 32 /** 33 * 34 * @Title: insertOrder 35 * @Description: 有序插入 36 * @return void 返回类型 37 * @author dongye 38 * @date 2016年3月1日 上午10:10:52 39 * @throws 40 */ 41 public void insertOrder(long value) { 42 int i; 43 for (i = 0; i < elements; i++) { 44 if(arr[i]>value){ 45 break; 46 } 47 } 48 for(int j=elements;j>i;j--){ 49 arr[j]=arr[j-1]; 50 } 51 arr[i]=value; 52 elements++; 53 } 54 55 56 57 58 /** 59 * 60 * @Title: display 61 * @Description: 显示元素 62 * @author dongye 63 * @date 2016年3月1日 上午9:32:25 64 */ 65 public void display(){ 66 System.out.print("["); 67 for (int i = 0; ipow){103 return -1;104 }else{105 if(value>arr[middle]){106 low=middle+1;107 }else{108 pow=middle-1;109 }110 }111 }112 }113 114 /**115 * 116 * @Description: 根据索引查找元素117 * @return long 返回类型 118 * @author dongye 119 * @date 2016年3月1日 上午9:40:59 120 */121 public long get(int index){122 if(index>=elements||index<=0){123 throw new ArrayIndexOutOfBoundsException();124 }else{125 return arr[index];126 }127 }128 129 /** 130 * @Title: delete 131 * @Description: 删除数据132 * @return void 返回类型 133 * @author dongye 134 * @date 2016年3月1日 上午9:41:57 135 * @throws 136 */137 public void delete(int index) {138 if(index>=elements||index<=0){139 throw new ArrayIndexOutOfBoundsException();140 }else{141 for (int i = index; i < elements; i++) {142 arr[index]=arr[index+1];143 elements--;144 }145 }146 }147 148 /**149 * 150 * @Title: change 151 * @Description: 更新数据 152 * @return void 返回类型 153 * @author dongye 154 * @date 2016年3月1日 上午9:45:27 155 * @throws156 */157 public void change(int index,int newValue){158 if(index>=elements||index<=0){159 throw new ArrayIndexOutOfBoundsException();160 }else{161 arr[index]=newValue;162 }163 }164 165 166 }