Java實現順序表

來源:互聯網
上載者:User

標籤:

利用順序儲存結構表示的順序表稱為順序表。

  它用一組連續的地址儲存單元一次存放線性表中的資料元素。

順序表的實現是資料結構中最簡單的一種。

由於代碼中已經有詳細注釋,代碼外不再闡述。

下次再陳上關於順序表的迴圈隊列和順序棧的代碼。

  1 package 線性表.順序表.普通數組;  2   3 /**  4  * ArrayList 順序表  5  * JAVA 3.0  6  * 拋異常處理錯誤的下標  7  * 使用泛型,當然如果要替換成Object也是可以替換  8  */  9 public class ArrayList<E> { 10  11     private int capacity; //數組的總容量 12     private int current;  //當前最後一個數組元素的下一個元素下標,從0開始,也是長度 13     private E[] data = null; //所有資料 14      15     public ArrayList(){ 16          17         this(10); 18     } 19      20     public ArrayList(int initialSize){ 21          22         init(initialSize); 23     } 24     /** 25      * 初始化數組 26      */ 27     @SuppressWarnings("unchecked") 28     public void init(int initialSize){ 29          30         current = 0; 31         data = (E[])new Object[initialSize];   32         capacity = initialSize; 33     } 34     /** 35      * 數組末尾插入元素*/ 36     public void add(E e){ 37          38         ensureCapacity(); 39         data[current] = e; 40         current++; 41          42     } 43     /** 44      * 在數組中插入元素*/ 45     public void insert(int index, E e){ 46          47         validateIndex(index); 48         ensureCapacity(); 49         for(int i=current;i>=index;i--){ 50              51             data[i+1] = data[i]; 52         } 53         data[index] = e; 54         current++; 55     } 56     /** 57      * 判斷是否需要擴充數組 58      * 如果需要將擴充為原來的兩倍 59      */ 60     @SuppressWarnings("unchecked") 61     private void ensureCapacity(){ 62          63         if(current == capacity){ 64                      65             capacity = capacity * 2; 66             E[] newData = (E[])new Object[capacity];   67             for(int index = 0; index < current; ++index) {   68                 newData[index] = data[index];   69             }   70             data = newData; 71         } 72     } 73      74     /** 75      * 刪除某個已經存在的對象 76      * 如果T在數組中,則刪除成功返回true,否則無這個元素返回false 77      */ 78     public boolean remove(E e){ 79          80         for(int i=0;i<current;i++){ 81              82             if(data[i].equals(e)){ 83                  84                 remove(i); 85                 return true; 86             } 87         } 88         return false; 89     } 90     /** 91      * 刪除特定下標的數組 92      * @param index 93      */ 94     public void remove(int index){ 95          96         validateIndex(index); 97         for(int i=index;i<current;i++){ 98              99             data[i] = data[i+1];100         }101         data[current] = null;102         current--;103     }104     /**105      * 修改下標為index的值*/106     public void set(int index, E e){107         108         validateIndex(index);109         data[index] = e;110     }111     /**112      * 擷取下標為index的值113      */114     public E get(int index){115         116         validateIndex(index);117         return data[index];118     }119     /**120      * 擷取數組已使用的個數121      */122     public int size(){123         124         return current;125     }126     /**127      * 銷毀數組所有元素128      */129     public void clear(){130         131 //      Arrays.fill(data, null);  可以替代下面的for迴圈132         for(int i=0;i<current;i++){133             134             data[i] = null;135         }136         capacity = 0;137         current = 0;138     }139     /**140      * 判斷數組是否為空白141      */142     public boolean isEmpty(){143         144         return current==0;145     }146     /**147      * 列印結構148      */149     public String toString() {  150         151         String str = "[ ";  152         for (Object o : data) {  153             if (o != null) {  154                 str += o + " ";  155             }  156         }  157         str += "]";  158         return str;  159     }  160     /**    * 判斷index 是否越界   */161     private void validateIndex(int index) {  162         163         if (index < 0 || index >= current) {  164             throw new IndexOutOfBoundsException("無效的下標:" + index);  165         }  166     }  167     

 

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.