public void copyList(ArrayListClass otherList){
if(this!=otherList){
for(int i=0;i<length;i++)
list[i]=null;
System.gc();
maxSize=otherList.maxSize;
length=otherList.length;
list=new DataElement[maxSize];
for(int j=0;j<length;j++)
list[j]=otherList.list[j].getCopy();
}
}
public abstract int seqSearch(DataElement seqItem);
public abstract void insert(DataElement insertItem);
public abstract void remove(DataElement removeItem);
}
看到代码的最后你回发现这个类其实是一个抽象类,为什么要这样定义呢?之所以这样我们是为了针对不同是类型:顺序表和非顺序表.不难想象他们的一些方法是存在差异的,先看一下非顺序表:
public class UnorderedArrayList extends ArrayListClass{
/**
*
*/
public UnorderedArrayList() {
super();
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see ArrayListClass#seqSearch(DataElement)
*/
public int seqSearch(DataElement seqItem) {
// TODO Auto-generated method stub
int loc;
boolean found=false;
for(loc=0;loc<length;loc++)
if(list[loc].equals(seqItem))
{
found=true;
break;
}
if(found)
return loc;
else
return -1;
}
/* (non-Javadoc)
* @see ArrayListClass#insert(DataElement)
*/
public void insert(DataElement insertItem) {
// TODO Auto-generated method stub
int loc;
if(length==0)
list[length++]=insertItem.getCopy();
else
if(length==maxSize)
System.err.println("Can’t insert in a full list!!");
else{
loc=seqSearch(insertItem);
if(loc==-1)
list[length++]=insertItem.getCopy();
else
System.err.println("The item to be inserted is allready in the list!!");
}
}
/* (non-Javadoc)
* @see ArrayListClass#remove(DataElement)
*/
public void remove(DataElement removeItem) {
// TODO Auto-generated method stub
int loc;
if(length==0)
System.err.println("Can’t delete from a empty list!!");
else{
loc=seqSearch(removeItem);
if(loc!=-1)
removeAt(loc);
else
System.err.println("The item to be deleted is not in the list!!");
}
}
}
就是这么简单!!相信顺序表也可以轻松高顶了.

