1,引入了JAVA泛型類,因此定義了一個Object[] 類型的數組,從而可以保存各種不同類型的對象。
2,默認構造方法創建了一個默認大小為16的Object數組;帶參數的構造方法創建一個指定長度的Object數組
3,實現的順序表的基本操作有:返回表的長度、獲取指定索引處的元素(注意是索引,而不是位置。索引以下標0開始,位置以下標1開始)、按值查找數據元素的位置、直接插入元素(順序表尾部)、向指定位置插入元素、直接刪除元素(在順序表尾部)、刪除指定索引處元素、判斷表是否為空、清空表。
4,在Java類庫中,java.util.ArrayList 類 實現了順序表,因此可以直接使用JAVA類庫中的ArrayList來完成順序表的各種操作。以下為實現順序表的具體代碼:
1 import java.util.Arrays; 2 3 public class SequenceList<T> { 4 private final int DEFAULT_SIZE = 16;//final實例變量顯示指定初始值,且不再變化。 5 6 private Object[] elementData;//該數組用來保存順序表中的元素 7 private int capacity;//保存數組的長度 8 private int size;//保存順序表中當前元素的個數 9 10 //以默認的大小創建順序表 11 public SequenceList(){ 12 capacity = DEFAULT_SIZE; 13 elementData = new Object[capacity]; 14 } 15 16 //以指定的大小創建順序表 17 public SequenceList(int initSize){ 18 capacity = 1; 19 while(capacity < initSize) 20 capacity <<= 1;//將capacity設置成大於initSize的最小2次方 21 elementData = new Object[capacity]; 22 } 23 24 //獲取順序表中當前元素的個數 25 public int length(){ 26 return size; 27 } 28 29 //獲取順序表中索引為 i 處的元素,i表示索引,即以 0 開始 30 public T get(int i){ 31 if(i < 0 || i > size - 1) 32 throw new IndexOutOfBoundsException("順序表索引越界"); 33 return (T)elementData[i]; 34 } 35 36 //查看順序表中指定元素的索引,若未找到,返回-1 37 public int locate(T element){ 38 for(int i = 0; i < size; i++) 39 if(elementData[i].equals(element)) 40 return i; 41 return -1; 42 } 43 44 //在順序表的指定索引處插入一個元素 45 public void insert(T element, int index){ 46 if(index < 0 || index > size) 47 throw new IndexOutOfBoundsException("順序表索引越界"); 48 ensureCapacity(size + 1);//確保順序表滿時進行擴容,從而能插入元素 49 //將指定索引后的所有元素向后移動一個位置 50 // System.arraycopy(elementData, index, elementData, index + 1, size - index); 51 for(int i = size; i > index; i--) 52 elementData[i] = elementData[i - 1]; 53 elementData[index] = element; 54 size++;//順序表中的元素個數增1 55 } 56 57 private void ensureCapacity(int minCapacity){ 58 //當數組容量已滿時,對數組進行擴容。將容量擴展到大於minCapacity的最小2的次方 59 if(minCapacity > capacity){ 60 while(capacity < minCapacity) 61 capacity <<= 1; 62 elementData = Arrays.copyOf(elementData, capacity); 63 } 64 } 65 66 //在順序表的末尾添加一個元素 67 public void add(T element){ 68 insert(element, size); 69 } 70 71 //刪除順序表中指定索引處的元素 72 public T delete(int index){ 73 if(index < 0 || index > size - 1) 74 throw new IndexOutOfBoundsException("順序表索引越界"); 75 T oldValue = (T)elementData[index]; 76 int numMoved = size - index - 1;//計算需要移動的元素個數 77 if(numMoved > 0){ 78 System.arraycopy(elementData, index + 1, elementData, index, numMoved); 79 } 80 elementData[--size] = null;//讓垃圾回收器及時回收,避免內存泄露 81 return oldValue; 82 } 83 84 //刪除順序表中的最后一個元素 85 public T remove(){ 86 return delete(size - 1); 87 } 88 89 //判斷順序表是否為空表 90 public boolean empty(){ 91 return size == 0; 92 } 93 94 //清空順序表 95 public void clear(){ 96 Arrays.fill(elementData, null);//將數組elementData中的每個元素都賦值null 97 size = 0; 98 } 99 100 public String toString(){ 101 if(size == 0) 102 return "[]"; 103 else{ 104 StringBuilder sb = new StringBuilder("["); 105 for(int i = 0; i < size; i++) 106 sb.append(elementData[i].toString() + ", "); 107 int len = sb.length(); 108 //刪除由於上面for循環中最后添加的多余的兩個字符 (一個是逗號,一個是空格符號) 109 return sb.delete(len - 2, len).append("]").toString(); 110 } 111 } 112 }