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];
}
}