Java實現自定義數組及其方法

自定義數組數組

主要功能有增、刪(根據索引,根據值)、改、查擴容等功能函數

 1 package array;  2 
 3 public class CustomArray {  4     private int[] array = null;  5     //數組有效長度
 6     public int length = 0;  7 
 8     //空參構造函數,默認數組大小爲10
 9     public CustomArray() {  10         this.array = new int[10];  11  }  12 
 13     public CustomArray(int size) {  14         this.array = new int[size];  15  }  16 
 17     //給自定義數組添加元素
 18     public void insert(int number) {  19         //判斷數組是否滿  20         //滿了,擴容,擴容須要新建一個數組,將舊的數據複製過去,再插入  21         //沒滿,直接插入  22         //插入以後length+1
 23         if (length == array.length) {  24             expand(this.array);  25             array[length] = number;  26         } else {  27             this.array[length] = number;  28  }  29         length++;  30 
 31  }  32 
 33     //根據索引刪除元素
 34     public void deleteByIndex(int index) throws Exception {  35         //判斷索引是否越界,即超過了有效長度  36         //超過了,拋出異常提示  37         //沒超過就刪除  38             //首先須要將該索引以後的全部元素前移一個位置。  39             //最後length-1
 40         if (index > length - 1 || index < 0) {  41             throw new Exception("刪除時索引越界");  42         } else {  43             for (int i = index; i < length; i++) {  44                 array[i] = array[i + 1];  45  }  46             length--;  47  }  48  }  49 
 50     //根據值刪除元素,刪除多個
 51     public void deleteByValue(int value) throws Exception {  52         //首先看數組中有沒有這個值,沒有拋異常提示
 53         boolean flag = false;  54         for (int i = 0; i < length; i++) {  55             if (array[i] == value) {  56                 flag = true;  57  deleteByIndex(i);  58  }  59  }  60         if (!flag)  61             throw new Exception("該元素不存在");  62  deleteOne(value);  63 
 64  }  65 
 66     //刪除一個元素
 67     public void deleteOne(int value) throws Exception {  68         boolean flag = false;  69         for (int i = 0; i < length; i++) {  70             if (array[i] == value) {  71                 flag = true;  72  deleteByIndex(i);  73                 break;  74  }  75  }  76         if (!flag)  77             throw new Exception("該元素不存在");  78 
 79  }  80 
 81 
 82     //修改某索引對應元素的值
 83     public void update(int index, int value) throws Exception {  84         if (index > length - 1 || index < 0) {  85             throw new Exception("修改時索引越界");  86         } else {  87             array[index] = value;  88  }  89  }  90 
 91     //(遍歷)數組的元素
 92     public void travel() {  93         System.out.print("[");  94         for (int i = 0; i < length; i++) {  95  System.out.print(array[i]);  96             if (i < length - 1)  97                 System.out.print(",");  98  }  99         System.out.println("]"); 100  } 101 
102     //根據值查找元素,返回索引
103     public int search(int value) throws Exception { 104         int i = 0; 105         for (i = 0; i < length; i++) { 106             if (value == array[i]) 107                 break; 108  } 109         if (i == length) 110             return -1; 111         return i; 112  } 113     //根據索引元素,返回值
114     public int searchByIndex(int index) throws Exception { 115         if(index<0||index>=length){ 116             throw new Exception("索引越界"); 117  } 118         return array[index]; 119 
120  } 121 
122     //每次擴容後比以前大一倍
123     public void expand(int[] arr) { 124         int expandSize = arr.length * 2; 125         this.array = new int[expandSize]; 126 
127         for (int i = 0; i < arr.length; i++) { 128             this.array[i] = arr[i]; 129  } 130  } 131 
132 
133 }

測試類以下:測試

 1 package array;  2 
 3 public class ArrayTest {  4 
 5     public static void main(String[] args) throws Exception {  6 
 7         CustomArray array=new CustomArray();  8         array.insert(10);  9         array.insert(9); 10         array.insert(8); 11         array.insert(4); 12         array.insert(5); 13         array.insert(6); 14         array.deleteByIndex(0); 15 
16  array.travel(); 17         
18  } 19 }

 

自定義有序數組this

主要功能有插入、二分查找遞歸版、二分查找非遞歸編碼

 1 package array;  2 
 3 public class OrderArray {  4     private int[] array = null;  5     //數組有效長度
 6     public int length = 0;  7 
 8     public OrderArray() {  9         this.array = new int[50]; 10  } 11 
12     public OrderArray(int size) { 13         this.array = new int[size]; 14  } 15 
16     //此處有許多細節
17     public void insert(int value) { 18 
19         int i; 20         for (i = 0; i < length; i++) { 21             if (value < array[i]) 22                 break; 23  } 24         for (int j = length - 1; j >=i; j--) { 25             array[j+1] = array[j]; 26  } 27         array[i] = value; 28 
29         length++; 30 
31  } 32 
33     public void travel() { 34         System.out.print("["); 35         for (int i = 0; i < length; i++) { 36  System.out.print(array[i]); 37             if (i < length - 1) 38                 System.out.print(","); 39  } 40         System.out.println("]"); 41  } 42     //二分查找,返回索引
43     public int binarySearch(int value){ 44         int left=0,right=length-1; 45         int mid; 46 
47         while(left<=right){ 48             mid=(left+right)/2; 49 
50             if(value==array[mid]) 51                 return mid; 52             if(value<array[mid]){ 53                 right=mid-1; 54             }else{ 55                 left=mid+1; 56  } 57  } 58         return -1; 59  } 60     public int binarySearchByRecursion(int value,int begin,int end){ 61         int mid=(begin+end)/2; 62 
63         if(array[mid]==value) 64             return mid; 65         if(begin>end) 66             return -1; 67         if(value<array[mid]){ 68             end=mid-1; 69             return binarySearchByRecursion(value,begin,end); 70         }else{ 71             begin=mid+1; 72             return binarySearchByRecursion(value,begin,end); 73  } 74  } 75 
76 
77 }

 

測試類:spa

 1 package array;  2 
 3 public class ArrayTest {  4 
 5 
 6     public static void main(String[] args) throws Exception {  7 
 8         OrderArray orderArray=new OrderArray();  9         
10         orderArray.insert(9); 11         orderArray.insert(8); 12         orderArray.insert(7); 13         orderArray.insert(6); 14         orderArray.insert(5); 15  orderArray.travel(); 16         System.out.println(orderArray.binarySearch(6)); 17         System.out.println(orderArray.binarySearchByRecursion(6,0,orderArray.length-1)); 18 
19  } 20 
21 }

經過以上練習能夠很好的鞏固基礎編碼能力code

 

冰凍三尺非一日之寒,腳踏實地埋頭幹blog

相關文章
相關標籤/搜索