Java数据结构---基于数组的表__教程 |
|
日期:2007-5-20 0:37:03 人气:88 [大 中 小] |
|
|
|
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 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) |
|
出处:本站原创 作者:佚名 |
|
|