使用JAVA數組實現順序表

來源:互聯網
上載者:User

標籤:

1,引入了JAVA泛型類,因此定義了一個Object[] 類型的數組,從而可以儲存各種不同類型的對象。

2,預設構造方法建立了一個預設大小為16的Object數組;帶參數的構造方法建立一個指定長度的Object數組

3,實現的順序表的基本操作有:返回表的長度、擷取指定索引處的元素(注意是索引,而不是位置。索引以下標0開始,位置以下標1開始)、按值尋找資料元素的位置、直接插入元素(順序表尾部)、向指定位置插入元素、直接刪除元素(在順序表尾部)、刪除指定索引處元素、判斷表是否為空白、清空表。

  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 }

 

使用JAVA數組實現順序表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.