本篇文章,使用java語言,封裝一個數組工具類。不使用系統提供的api。
包括一些數組的增刪改查,有序添加,二分查找等功能。
package ch01;public class MyArray { // 底層內部的數組 PRivate long[] arr; // 數組容量,記錄用戶使用數組的長度 private int elements; /** * 無參構造,設置默認數組大小。 */ public MyArray() { arr = new long[50]; } /** * 帶參構造,用戶指定數組大小 * * @param size */ public MyArray(int size) { arr = new long[size]; } /** * 往數組增加元素(增) */ public void insert(int value) { arr[elements] = value; // 添加元素,容器記錄值也要增加 elements++; } /** * 在指定位置添加元素 * * @param index * @param value */ public void insert(int index, int value) { // 創建新的數組,比原來數組長度多1 long[] tempArray = new long[elements + 1]; //新插入的位置,就制定添加在索引處 tempArray[index] = value; // 判斷數組角標越界 if (index < 0 || index >= elements) { throw new ArrayIndexOutOfBoundsException(); } else { for (int i = 0; i < elements; i++) { if (i < index) { //原來數組索引左側全部給新數組左側 tempArray[i] = arr[i]; } else { //原來數組索引位置往后全部賦值給新數組索引后邊的位置 tempArray[i + 1] = arr[i]; } } } // 把最新數組賦值給最初的數組 arr = tempArray; elements++; } /** * 添加的數據,默認自然排序 * @param value */ public void insertOrder(long value){ int i = 0; for (i = 0; i < elements; i++) { //比較,添加的元素第一次小于數組位置元素時,跳出循環,該i值位置就是添加元素的位置 if(value < arr[i]){ break;//跳出循環后,i的值保留,就是要添加數據的數組索引位置 } } //倒著遍歷 for (int j = elements; j > i; j--) { //原來數組i位置以后的數組,重新 以次 往后一個位置賦值。這樣才可以保證添加元素在最小位置,保證排序 arr[j] = arr[j-1]; } //把添加元素,加載指定位置 arr[i] = value; //添加一次,容器標記加一 elements++; } /** * 遍歷數組 */ public void disPlay() { System.out.print("["); for (int i = 0; i < elements; i++) { if (i == elements - 1) { System.out.println(arr[i] + "]"); } else { System.out.print(arr[i] + " "); } } } /** * 根據索引,獲取索引位置的值 * * @param index * @return */ public long get(int index) { // 判斷數組角標越界 if (index < 0 || index >= elements) { throw new ArrayIndexOutOfBoundsException(); } else { return arr[index]; } } /** * 刪除指定索引位置的值 * * @param index */ public void remove(int index) { // 判斷數組角標越界 if (index < 0 || index >= elements) { throw new ArrayIndexOutOfBoundsException(); } else { for (int i = index; i < elements; i++) { // 把index位置的數據替換掉了 arr[i] = arr[i + 1]; } elements--; } } /** * 修改某個位置的數據 * * @param index */ public void upData(int index, int value) { // 判斷數組角標越界 if (index < 0 || index >= elements) { throw new ArrayIndexOutOfBoundsException(); } else { arr[index] = value; } } /** * 根據值找出對應的索引,找不到就返回-1 * 線性查找。 * @param value * @return */ public int search(int value) { int index = -1; for (int i = 0; i < elements; i++) { if (arr[i] == value) { index = i; } } return index; } /** * 二分查找,超找元素的索引。 * 前提:必須是有序數組。無需數組通過上述線性查找方式 * @param value * @return */ public int binarySearch(long value){ /*1、記錄最大索引、最小索引 2、計算中間索引 3、比較中間索引值與添加元素 相等 : 直接返回 不相同 大了 往左查找 max = middle-1; 小了 往右查找 min = middle+1; 返回2 當min > max 的時候,跳出循環表示查找不到數據。返回-1。*/ int max = elements-1; int min = 0; int middle = 0; while(true){ middle = (max+min)/2; // 相等 : 直接返回 if(arr[middle] == value){ return middle; }else if(arr[middle] > value){ //大了 左邊查找 max = middle -1; }else { //小了 往右查找 min = middle +1; } if(min > max){ return -1; } } }}微信搜索公眾號 codeHoward 在公眾號可以最早獲取博客最新更新消息,以及了解一些大公司面試經驗。
新聞熱點
疑難解答