1、具體見注釋
2、后續或有更新
1 public class MyArray { 2 private long[] array; 3 private int cnt; // 自定義數組類的元素個數 4 5 /** 6 使用自定義類封裝數組,添加類方法實現數據操作 7 */ 8 public MyArray() { 9 array = new long[50]; 10 } 11 12 public MyArray(int size) { 13 array = new long[size]; 14 } 15 16 /** 17 插入數據,返回值為空 18 */ 19 public void insert(long insertValue) { 20 array[cnt++] = insertValue; 21 } 22 23 /** 24 顯示數據,返回值為空 25 */ 26 public void display() { 27 System.out.print("["); 28 for (int i = 0; i < cnt ; ++i) { 29 System.out.print(array[i]); 30 if (i != cnt - 1) { 31 System.out.print(","); 32 } 33 } 34 System.out.println("]"); 35 } 36 37 /** 38 按值查找數據,返回索引值 39 算法:線性查找 40 */ 41 public int search(long targetValue) { 42 int i; 43 int searchResult; 44 for (i = 0; i < cnt; ++i) { 45 if (targetValue == array[i]) { 46 break; 47 } 48 } 49 if (i == cnt) { 50 searchResult = -1; 51 } else { 52 searchResult = i; 53 } 54 return searchResult; // 保持單一出口 55 56 } 57 58 /** 59 按索引查找數據,返回值為目標數據 60 */ 61 public long get(int targetIndex) { 62 if (targetIndex < 0 || targetIndex >= cnt) { 63 throw new ArrayIndexOutOfBoundsException(); 64 } else { 65 return array[targetIndex]; 66 } 67 } 68 69 /** 70 按值刪除數據,返回其索引值 71 */ 72 public int deleteByValue(long deleteValue) { 73 int i; 74 int deleteResult; 75 for (i = 0; i < cnt; ++i) { 76 if (array[i] == deleteValue) { 77 int j; 78 for (j = i; j < cnt-1; ++j) { 79 array[j] = array[j+1]; 80 } 81 array[j] = array[--cnt]; 82 break; // 僅刪除從左到右第一個找到的目標值 83 } 84 } 85 if (i == cnt) { 86 deleteResult = -1; 87 } else { 88 deleteResult = i; 89 } 90 return deleteResult; // 保持單一出口 91 92 } 93 94 95 /** 96 按索引刪除數據,返回值為空 97 */ 98 public void delete(int index) { 99 if (index < 0 || index >= cnt) { 100 throw new ArrayIndexOutOfBoundsException(); 101 } else { 102 int i; 103 for (i = index; i < cnt - 1; ++i) { 104 array[i] = array[i + 1]; 105 } 106 //array[i] = array[cnt - 1]; 107 //cnt--; 108 109 array[i] = array[--cnt]; // 替換上兩行 110 } 111 } 112 113 /** 114 根據索引值,更新數據,返回值為空 115 */ 116 public void update(int index, int newValue) { 117 if (index < 0 || index >= cnt) { 118 throw new ArrayIndexOutOfBoundsException(); 119 } else { 120 array[index] = newValue; 121 } 122 } 123 124 public static void main(String[] args) { 125 MyArray array = new MyArray(3); 126 array.insert(13); 127 array.insert(34); 128 array.insert(90); 129 130 array.display(); 131 132 array.deleteByValue(34); 133 134 array.display(); 135 136 } 137 138 }
3、添加自定義有序數組類
1 public class MyOrderArray { 2 private long[] array; 3 private int cnt; // 自定義數組類的元素個數 4 5 /** 6 使用自定義類封裝數組,添加類方法實現數據操作 7 */ 8 public MyOrderArray() { 9 array = new long[50]; 10 } 11 12 public MyOrderArray(int size) { 13 array = new long[size]; 14 } 15 16 /** 17 按序插入數據,返回值為空 18 */ 19 public void insert(long insertValue) { 20 int i; 21 for (i = 0; i < cnt; ++i) { 22 if (array[i] > insertValue) { 23 break; 24 } 25 } 26 int j; 27 for (j = cnt; j > i; --j) { 28 array[j] = array[j - 1]; 29 } 30 array[i] = insertValue; 31 cnt++; 32 } 33 34 /** 35 顯示數據,返回值為空 36 */ 37 public void display() { 38 System.out.print("["); 39 for (int i = 0; i < cnt ; ++i) { 40 System.out.print(array[i]); 41 if (i != cnt - 1) { 42 System.out.print(","); 43 } 44 } 45 System.out.println("]"); 46 } 47 48 /** 49 按值查找數據,返回索引值 50 算法:線性查找 51 */ 52 public int search(long targetValue) { 53 int i; 54 int searchResult; 55 for (i = 0; i < cnt; ++i) { 56 if (targetValue == array[i]) { 57 break; 58 } 59 } 60 if (i == cnt) { 61 searchResult = -1; 62 } else { 63 searchResult = i; 64 } 65 return searchResult; // 保持單一出口 66 67 } 68 69 /** 70 按索引查找數據,返回值為目標數據 71 */ 72 public long get(int targetIndex) { 73 if (targetIndex < 0 || targetIndex >= cnt) { 74 throw new ArrayIndexOutOfBoundsException(); 75 } else { 76 return array[targetIndex]; 77 } 78 } 79 80 /** 81 按值刪除數據,返回其索引值 82 */ 83 public int deleteByValue(long deleteValue) { 84 int i; 85 int deleteResult; 86 for (i = 0; i < cnt; ++i) { 87 if (array[i] == deleteValue) { 88 int j; 89 for (j = i; j < cnt-1; ++j) { 90 array[j] = array[j+1]; 91 } 92 array[j] = array[--cnt]; 93 break; // 僅刪除從左到右第一個找到的目標值 94 } 95 } 96 if (i == cnt) { 97 deleteResult = -1; 98 } else { 99 deleteResult = i; 100 } 101 return deleteResult; // 保持單一出口 102 103 } 104 105 106 /** 107 按索引刪除數據,返回值為空 108 */ 109 public void delete(int index) { 110 if (index < 0 || index >= cnt) { 111 throw new ArrayIndexOutOfBoundsException(); 112 } else { 113 int i; 114 for (i = index; i < cnt - 1; ++i) { 115 array[i] = array[i + 1]; 116 } 117 //array[i] = array[cnt - 1]; 118 //cnt--; 119 120 array[i] = array[--cnt]; // 替換上兩行 121 } 122 } 123 124 /** 125 根據索引值,更新數據,返回值為空 126 */ 127 public void update(int index, int newValue) { 128 if (index < 0 || index >= cnt) { 129 throw new ArrayIndexOutOfBoundsException(); 130 } else { 131 array[index] = newValue; 132 } 133 } 134 135 public static void main(String[] args) { 136 MyOrderArray array = new MyOrderArray(3); 137 array.insert(90); 138 array.insert(13); 139 array.insert(34); 140 141 array.display(); 142 143 array.deleteByValue(34); 144 145 array.display(); 146 147 } 148 149 }
4、MyArray類與MyOrderArray類目前僅區別於insert方法,后續或有更新
5、MyOrderArray類新增二分查找方法binarySearch,具體細節見該方法代碼
1 public class MyOrderArray { 2 private long[] array; 3 private int cnt; // 自定義數組類的元素個數 4 5 /** 6 使用自定義類封裝數組,添加類方法實現數據操作 7 */ 8 public MyOrderArray() { 9 array = new long[50]; 10 } 11 12 public MyOrderArray(int size) { 13 array = new long[size]; 14 } 15 16 /** 17 按序插入數據,返回值為空 18 */ 19 public void insert(long insertValue) { 20 int i; 21 for (i = 0; i < cnt; ++i) { 22 if (array[i] > insertValue) { 23 break; 24 } 25 } 26 int j; 27 for (j = cnt; j > i; --j) { 28 array[j] = array[j - 1]; 29 } 30 array[i] = insertValue; 31 cnt++; 32 } 33 34 /** 35 顯示數據,返回值為空 36 */ 37 public void display() { 38 System.out.print("["); 39 for (int i = 0; i < cnt ; ++i) { 40 System.out.print(array[i]); 41 if (i != cnt - 1) { 42 System.out.print(","); 43 } 44 } 45 System.out.println("]"); 46 } 47 48 /** 49 按值查找數據,返回索引值 50 算法:線性查找 51 */ 52 public int search(long targetValue) { 53 int i; 54 int searchResult; 55 for (i = 0; i < cnt; ++i) { 56 if (targetValue == array[i]) { 57 break; 58 } 59 } 60 if (i == cnt) { 61 searchResult = -1; 62 } else { 63 searchResult = i; 64 } 65 return searchResult; // 保持單一出口 66 67 } 68 69 /** 70 按值查找數據,返回索引值 71 算法:二分查找 72 */ 73 public int binarySearch(long targetValue) { 74 int middle = 0; 75 int low = 0; 76 int top = cnt; 77 78 while (true) { 79 middle = (top + low) / 2; 80 if (targetValue == array[middle]) { 81 return middle; 82 } else if (low > top) { 83 return -1; 84 } else if (targetValue < array[middle]) { 85 top = middle - 1; // 切記減一 86 } else if (targetValue >= array[middle]) { 87 low = middle + 1; // 切記加一 88 } 89 } 90 } 91 92 /** 93 按索引查找數據,返回值為目標數據 94 */ 95 public long get(int targetIndex) { 96 if (targetIndex < 0 || targetIndex >= cnt) { 97 throw new ArrayIndexOutOfBoundsException(); 98 } else { 99 return array[targetIndex]; 100 } 101 } 102 103 /** 104 按值刪除數據,返回其索引值 105 */ 106 public int deleteByValue(long deleteValue) { 107 int i; 108 int deleteResult; 109 for (i = 0; i < cnt; ++i) { 110 if (array[i] == deleteValue) { 111 int j; 112 for (j = i; j < cnt-1; ++j) { 113 array[j] = array[j+1]; 114 } 115 array[j] = array[--cnt]; 116 break; // 僅刪除從左到右第一個找到的目標值 117 } 118 } 119 if (i == cnt) { 120 deleteResult = -1; 121 } else { 122 deleteResult = i; 123 } 124 return deleteResult; // 保持單一出口 125 126 } 127 128 /** 129 按索引刪除數據,返回值為空 130 */ 131 public void delete(int index) { 132 if (index < 0 || index >= cnt) { 133 throw new ArrayIndexOutOfBoundsException(); 134 } else { 135 int i; 136 for (i = index; i < cnt - 1; ++i) { 137 array[i] = array[i + 1]; 138 } 139 //array[i] = array[cnt - 1]; 140 //cnt--; 141 142 array[i] = array[--cnt]; // 替換上兩行 143 } 144 } 145 146 /** 147 根據索引值,更新數據,返回值為空 148 */ 149 public void update(int index, int newValue) { 150 if (index < 0 || index >= cnt) { 151 throw new ArrayIndexOutOfBoundsException(); 152 } else { 153 array[index] = newValue; 154 } 155 } 156 157 public static void main(String[] args) { 158 MyOrderArray array = new MyOrderArray(3); 159 array.insert(90); 160 array.insert(13); 161 array.insert(34); 162 163 array.display(); 164 165 //array.deleteByValue(34); 166 System.out.println(array.binarySearch(90)); 167 168 array.display(); 169 170 } 171 172 }