如图:
自定义数组越界异常:
/**
* @ClassName: OutOfBoundaryException
* @Description: TODO 自定义的数组越界的异常
* @author 萨摩耶
* @date 2018年4月29日 下午3:37:11
*
*/
@SuppressWarnings("serial")
public class OutOfBoundaryException extends Exception{
public OutOfBoundaryException(String message)
{
super(message);
}
}
List接口:
/**
* @ClassName: List
* @Description: TODO 线性表的接口
* @author 萨摩耶
* @date 2018年4月29日 下午3:31:41
*
*/
public interface List {
//返回线性表的大小,即数据元素的个数
public int getSize();
//如果线性表为空返回true否则返回false
public boolean isEmpty();
//判断线性表中是否包含数据元素e
public boolean contains(Object e);
//返回数据元素e在线性表中的位置
public int indexOf(Object e);
//将数据元素e插入到线性表中i号位置
public void insert(int i,Object e)throws OutOfBoundaryException;
//将数据元素e插入数据元素obj之前
public boolean insertBefore(Object obj,Object e);
//经数据元素e插入数据元素obj之后
public boolean insertAfter(Object obj,Object e);
//移除位置i的元素
public Object remove(int i)throws OutOfBoundaryException;
//删除线性表中第一个与e相同的元素
public boolean remove(Object e);
//替换线性表中序号为i的数据元素为e返回原数据元素
public Object replace(int i,Object e)throws OutOfBoundaryException;
//返回线性表中序号为i的数据元素
public Object get(int i) throws OutOfBoundaryException;
}
strategy配置:(利用Object类型,就会产生一个问题:int类型和String类型的比较)
public interface Strategy {
//判断两个数据元素是否相等
public boolean equal(Object obj1,Object obj2);
//比较两个数据元素的大小
public int compare(Object obj1,Object obj2);
}
List接口的实现(ListArray.java):
public class ListArray implements List{
private final int LEN=8;// 数组的默认大小
private Strategy strategy;//数据元素的比较策略
private int size;//线性表中数据元素的个数
private Object[] elements;//数据元素数组
public ListArray(Strategy strategy)
{
this.strategy=strategy;
this.size=0;
elements=new Object[LEN];
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return size;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return size==0;
}
@Override
public boolean contains(Object e) {
// TODO Auto-generated method stub
for(int i=0;i<size;i++)
{
if(strategy.equal(e,elements[i]))
return true;
}
return false;
}
@Override
public int indexOf(Object e) {
// TODO Auto-generated method stub
for(int i=0;i<size;i++)
{
if(strategy.equal(e,elements[i]))
return i;
}
return -1;
}
@Override
public void insert(int i, Object e) throws OutOfBoundaryException {
// TODO Auto-generated method stub
if(i<0||i>size)
throw new OutOfBoundaryException("越界" );
if(size>=elements.length)
expandSapce();
for(int j=size;j>i;j--)
elements[j]=elements[j-1];
elements[i]=e;
size++;
return;
}
public void expandSapce()
{
Object[] a=new Object[elements.length*2];
for(int i=0;i<elements.length;i++)
{
a[i]=elements[i];
}
elements=a;
}
@Override
public boolean insertBefore(Object obj, Object e) {
// TODO Auto-generated method stub
int i=indexOf(obj);
if(i<0) return false;
try {
insert(i,e);
} catch (OutOfBoundaryException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return true;
}
@Override
public boolean insertAfter(Object obj, Object e) {
// TODO Auto-generated method stub
int i=indexOf(obj);
if(i<0) return false;
try {
insert(i+1,e);
} catch (OutOfBoundaryException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return true;
}
@Override
public Object remove(int i) throws OutOfBoundaryException {
// TODO Auto-generated method stub
if(i<0||i>size)
throw new OutOfBoundaryException("越界" );
Object obj=elements[i];
for(int j=i;j<size;j++)
{
elements[j]=elements[j+1];
}
elements[--size]=null;
return obj;
}
@Override
public boolean remove(Object e) {
// TODO Auto-generated method stub
int i=indexOf(e);
if(i<0) return false;
try {
remove(i);
} catch (OutOfBoundaryException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return true;
}
@Override
public Object replace(int i, Object e) throws OutOfBoundaryException {
// TODO Auto-generated method stub
if(i<0||i>size)
throw new OutOfBoundaryException("越界" );
Object obj=elements[i];
elements[i]=e;
return obj;
}
@Override
public Object get(int i) throws OutOfBoundaryException {
// TODO Auto-generated method stub
if(i<0||i>size)
throw new OutOfBoundaryException("越界" );
return elements[i];
}
}
策略的实现:
/**
* @ClassName: IntergerStretegy
* @Description: TODO 整数的比较策略
* @author 萨摩耶
* @date 2018年4月30日 上午9:24:42
*
*/
public class IntergerStretegy implements Strategy{
@Override
public boolean equal(Object obj1, Object obj2) {
// TODO Auto-generated method stub
if(obj1 instanceof Integer&&obj2 instanceof Integer)
{
if(obj1==obj2)
return true;
}
return false;
}
@Override
public int compare(Object obj1, Object obj2) {
// TODO Auto-generated method stub
return 0;
}
}
测试类:
public class Test {
public static void main(String[] args) throws OutOfBoundaryException
{
ListArray la=new ListArray(new IntergerStretegy());
for(int i=0;i<7;i++)
la.insert(i, i+1);
System.out.println(la.get(6));
System.out.println(la.indexOf(5));
}
}
原文地址:http://blog.51cto.com/12666319/2113786