线性表文件目录
List.java 文件接口
package com.data.service;
import com.data.util.OutOfBoundaryException;
public interface List {
/**
* 返回线性表的大小,即元素的个数
* @return
*/
public int getSize();
/**
* 如果线性表为空返回true,否则返回false
*/
public boolean isEmpty();
/**
* 判断线性表是否包含数据元素e,包含返回true,否则返回false
* @param e
*/
public boolean contains(Object e);
/**
* 返回数据元素e在线性表中的序号,如果e不存在则返回-1
* @param e
*/
public int indexOf(Object e);
/**
* 将数据元素e插入到线性表中i号的位置,若i越界则抛异常
* @param index
* @param e
*/
public void insert(int index, Object e) throws OutOfBoundaryException;
/**
* 将数据元素e插入到数据元素p之前,成功返回true否则返回false
* @param p
* @param e
*/
public boolean insertBefore(Object p, Object e);
/**
* 将数据元素e插入到数据元素p之后,成功返回true否则返回false
* @param p
* @param e
* @return
*/
public boolean insertAfter(Object p, Object e);
/**
* 删除线性表中序号为i的元素,并返回这个元素;若i越界则抛异常
* @param index
* @return
*/
public Object remove(int index) throws OutOfBoundaryException;
/**
* 删除线性表中第一个与e相同的数据元素,成功返回true否则返回false
* @param e
* @return
*/
public boolean remove(Object e);
/**
* 替换线性表中序号为i的数据元素为e,返回原数据元素。若i越界则抛异常
* @param index
* @param e
* @return
*/
public Object replace(int index, Object e) throws OutOfBoundaryException;
/**
* 返回线性表中序号为i的与数据元素。若i越界则抛异常
* @param index
* @return
*/
public Object get(int index) throws OutOfBoundaryException;
}
Strategy.java 文件接口
package com.data.service;
public interface Strategy {
/**
* 判断两个数据元素是否相等
* @param obj1
* @param obj2
* @return
*/
public boolean equal(Object obj1, Object obj2);
/**
* 比较连个数据元素的大小
* 如果obj1 < obj2 返回-1
* 如果obj1 = obj2 返回0
* 如果obj1 > obj2 返回1
* @param obj1
* @param obj2
* @return
*/
public int compare(Object obj1, Object obj2);
}
ListImp.java 文件接口
package com.data.service.imp;
import com.data.service.List;
import com.data.util.OutOfBoundaryException;
public class ListImp implements List {
private final int LIST_LEN = 8; //数组默认大小
private StrategyImp strategyImp; //数据元素比较策略
private Object[] elements; //数据元素数组
private int size; //线性表中数据元素的个数
/******* 构造方法 ********/
public ListImp() {
this(new StrategyImp());
}
public ListImp(StrategyImp strategyImp) {
this.strategyImp = strategyImp;this.size = 0;this.elements = new Object[LIST_LEN];}/******* 私有方法 ********//** * 扩充数组容量 */private void expandSpace() {//创建一个数组容量是旧数组的两倍大小Object[] a = new Object[this.elements.length*2];//遍历旧数组把值都赋值到新数组中for (int i=0; i=this.getSize()) {throw new OutOfBoundaryException("错误!指定的插入序号越界");}//如果当前列表的大小大于的容量则进行扩充容量处理if (this.getSize() >= this.elements.length) {this.expandSpace();}//遍历列表所有元素直到序号等于传入的序号为止for (int i=this.getSize(); i>index; i--) {//列表想向后移位知道等于传入的序号为止this.elements[i] = this.elements[i-1];}//把新的数据元素赋值给传入的序号位置this.elements[index] = e;//列表的大小加一size++;}@Overridepublic boolean insertBefore(Object p, Object e) {//获取元素在列表中的序号int index = this.indexOf(p);if (index<0) {return false;}this.insert(index, e);return true;}@Overridepublic boolean insertAfter(Object p, Object e) {//获取元素在列表中的序号int index = this.indexOf(p);if (index<0) {return false;}this.insert(index+1, e);return true;}@Overridepublic Object remove(int index) throws OutOfBoundaryException {//容错处理如果传入的序号小于0或者大于当前列表的大小则表示越界if (index<0||index>=this.getSize()) {throw new OutOfBoundaryException("错误!指定的插入序号越界");}//获取列表中被传入的序号对应的数据元素Object obj = this.elements[index];//从被传入的序号开始进行遍历之后的元素for (int i=index; i=this.getSize()) {
throw new OutOfBoundaryException("错误!指定的插入序号越界");
}
//把被传入序号对应的数据元素赋值给临时变量
Object obj = this.elements[index];
//把被传入的序号位置的数据元素重新赋值为元素e
this.elements[index] = e;
//返回被替换的数据元素
return obj;
}
@Override
public Object get(int index) throws OutOfBoundaryException {
//容错处理如果传入的序号小于0或者大于当前列表的大小则表示越界
if (index<0||index>=this.getSize()) {
throw new OutOfBoundaryException("错误!指定的插入序号越界");
}
//从列表中获取被传入序号对应的数据元素
return this.elements[index];
}
}