Java数据结构---基于数组的表__教程 |
|
日期:2007-5-20 0:37:03 人气:88 [大 中 小] |
|
|
|
protected DataElement[] list;这就是它的所有域了.
接下来就是它的方法了,我们对表的操作应该有很多种,比如插入、查询、删减等等,我们要逐个的实现,具体方法不再赘述,且看最后完成代码
public abstract class ArrayListClass { //fields protected int length; protected int maxSize; protected DataElement[] list; //defalt constructors public ArrayListClass(){ length=0; maxSize=100; list=new DataElement[maxSize]; } //constructors public ArrayListClass(int size){ if(size<=0){ System.err.println("The arry size must be positive.Creating an array of size 100."); maxSize=100; } else maxSize=size; length=0; list=new DataElement[maxSize]; } public ArrayListClass(ArrayListClass otherList){ maxSize=otherList.maxSize; length=otherList.length; list=new DataElement[maxSize]; for(int i=0;i list[i]=otherList.list[i].getCopy(); } } //methods public boolean isEmpty(){ return (length==0); } public boolean isFull(){ return (length==maxSize); } public int listSize(){ return length; } public int maxListSize(){ return maxSize; } public void print(){ for(int i=0;i System.out.print(list[i]+" "); } System.out.println(); } public boolean isItemAtEqual(int location,DataElement item){ return(list[location].equals(item)); } public void insrtAt(int location,DataElement insertItem){ if(location<0||location>+maxSize){ System.out.println("The position of the item to be inserted is out of range!!"); } else if(length>=maxSize) System.err.println("Can't insert in a full list!!"); else{ for(int i=length;i>location;i--){ list[i]=list[i-1]; } list[location]=insertItem.getCopy(); length++; } } public void insertEnd(DataElement insertItem){ if(length>=maxSize){ |
|
出处:本站原创 作者:佚名 |
|
|