Java資料結構(線性表-->順序表簡單實現)

來源:互聯網
上載者:User

標籤:元素   ++   lin   ini   size   java資料結構   listt   public   線性表   

線性表是一種可以在任意位置插入和刪除元素,由n個同類型元素組成的線性結構。主要包括順序表,單鏈表,迴圈單鏈表,雙向鏈表和模擬鏈表。應用比較廣泛的是順序表和單鏈表。  2 下面是線性表的介面,主要操作包括插入元素,刪除元素,取得元素,得到線性表元素個數,判斷線性表是否為空白。  3 package com.linear.table;  4 /**  5  * 操作順序表的介面方法  6  * @author Mature  7  *  8  */  9 public interface MatureListInterface { 10 public void add(Object object) throws Exception; 11 public void remove(int index) throws Exception; 12 public Object get(int index) throws Exception; 13 public boolean isEmpty(); 14 public int size(); 15 public void initList(int size); 16  17 } 18  19 順序表的實現如下: 20 package com.linear.table; 21 /** 22  *  23  * @author Mature 24  * 25  */ 26 public class MatureList implements MatureListInterface { 27     int defaultSize = 10;// 預設容量 28     int size = 0;// 數組元素個數 29  30     Object[] objectList = null;// 儲存元素數組 31  32     MatureList() {// 預設容量構造器 33         initList(defaultSize); 34     } 35  36     public MatureList(int listSize) {// 指定容量構造器 37         this.defaultSize = listSize; 38         initList(defaultSize); 39  40     } 41  42     @Override 43     public void initList(int size) { 44  45         objectList = new Object[size]; 46  47     } 48  49     /** 50      * 添加一個元素(從1開始) 51      */ 52     @Override 53     public void add(Object object) throws Exception { 54         if (size == defaultSize) { 55             throw new Exception("順序表已滿,不能繼續插入"); 56  57         } else { 58  59             objectList[size] = object;// 進行數組添加 60             size++; 61         } 62  63     } 64  65     /** 66      * 刪除指定元素(從1開始) 67      */ 68     @Override 69     public void remove(int index) throws Exception { 70         Object[] objectListTmp = new Object[defaultSize];// 建立一個tmp數組 71         if (index < 0 || index > defaultSize || index > size) { 72             throw new Exception("參數錯誤,remove數組越界"); 73  74         } else { 75  76             for (int i = index; i <= size - 1; i++) { 77  78                 objectList[i - 1] = objectList[i]; 79  80             } 81             System.arraycopy(objectList, 0, objectListTmp, 0, size - 1);// 進行數組複製, 82             objectList = null; 83             this.objectList = objectListTmp;// 將新的數組複製到objectList 84             objectListTmp = null; 85             size--; 86         } 87  88     } 89  90     /** 91      * 擷取指定元素(從1開始) 92      */ 93     @Override 94     public Object get(int index) throws Exception { 95         if (index > defaultSize || index < 0) { 96             throw new Exception("參數錯誤,get數組越界"); 97  98         } else { 99             return objectList[index - 1];100         }101 102     }103 104     /**105      * 判斷線性表(順序表)是否為空白106      */107     @Override108     public boolean isEmpty() {109         if (objectList.length == 0) {110 111             return true;112         } else {113             return false;114 115         }116 117     }118 119     /**120      * 返回線性表(順序表)大小121      */122     @Override123     public int size() {124 125         return size;126     }127 128 }129 130 測試類別:131 package com.linear.table;132 /**133  * 134  * @author Mature135  *測試類別136  */137 public class Test {138 public static void main(String[] args) throws Exception {139     MatureList matureList=new MatureList(10);140     /**141      * 添加5個元素142      */143     matureList.add("mature1");144     matureList.add("mature2");145     matureList.add("mature3");146     matureList.add("mature4");147     matureList.add("mature5");148     System.out.println("元素個數:"+matureList.size);149     System.out.println("元素1:"+matureList.get(1));150     System.out.println("元素2:"+matureList.get(2));151     System.out.println("元素3:"+matureList.get(3));152     System.out.println("刪除元素3");153     matureList.remove(3);//刪除元素1154     System.out.println("元素個數:"+matureList.size);155     System.out.println("遍曆:");156     for(int i=1;i<=matureList.size;i++){157         System.out.println("元素:"+matureList.get(i));158         159     }160 }161 }162 163 測試效果:164 元素個數:5165 元素1:mature1166 元素2:mature2167 元素3:mature3168 刪除元素3169 元素個數:4170 遍曆:171 元素:mature1172 元素:mature2173 元素:mature4174 元素:mature5

 

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.