Java数据结构,线性表-->顺序表简单实现

线性表是一种可以在任意位置插入和删除元素,由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     @Override
108     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     @Override
123     public int size() {
124 
125         return size;
126     }
127 
128 }
129 
130 测试类:
131 package com.linear.table;
132 /**
133  * 
134  * @author Mature
135  *测试类
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);//删除元素1
154     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 元素个数:5
165 元素1:mature1
166 元素2:mature2
167 元素3:mature3
168 删除元素3
169 元素个数:4
170 遍历:
171 元素:mature1
172 元素:mature2
173 元素:mature4
174 元素:mature5