IList
package List;
public interface IList {
public void clear();
public boolean isEmpty();
public int length();
public Object get(int i) throws Exception;
/**
* @description 在线性表的第i个元素之前插入一个值为x的数据元素
* @param i 0<=i<=length()
* @param x
* @throws Exception
* @time 2015年12月22日 下午9:21:24
*/
public void insert(int i, Object x) throws Exception;
public void remove(int i) throws Exception;
public int indexOf(Object x);
public void display();
}
- 在线性表中逻辑上相邻的数据元素,在物理存储位置上也是相邻的。
- 存储密度高,但要预先分配,可能会造成空间的浪费。
- 便于随机存取
- 不便于插入和删除操作,会引起大量的数据元素的移动
SqList
package List;
public class SqList implements IList {
private Object[] listElem;
private int curLen;
/**
* @description 构造一个存储空间容量为maxSize的线性表
* @param maxSize
* @time 2015年12月22日 下午9:50:05
*/
public SqList(int maxSize) {
curLen = 0;
listElem = new Object[maxSize];
}
public void clear() {
curLen = 0;
}
public boolean isEmpty() {
return curLen == 0;
}
public int length() {
return curLen;
}
/**
* @description 读取线性表的第i个元素并返回其值
* @param i 0<=i<=length()
* @return
* @throws Exception
* @time 2015年12月22日 下午9:50:21
*/
public Object get(int i) throws Exception {
if (i < 0 || i > curLen - 1)
throw new Exception("第" + i + "个元素不存在.");
return listElem[i];
}
/**
* @description 在线性表的第i个元素之前插入一个值为x的数据元素
* @param i
* @param x
* @throws Exception
* @time 2015年12月22日 下午9:50:34
*/
public void insert(int i, Object x) throws Exception {
if (curLen == listElem.length)
throw new Exception("顺序表已满");
if (i < 0 || i > curLen)