接下来就是它的方法了,我们对表的操作应该有很多种,比如插入、查询、删减等等,我们要逐个的实现,具体方法不再赘述,且看最后完成代码
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<length;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<length;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){
System.err.println("Can't insert in a full list!!");
}
else{
list[length]=insertItem.getCopy();
length ;
}
}
public void removeAt(int location){
if(location<0||location>=length){
System.err.println("The location you want to remove is out of range!!");
}
else{
for(int i=location;i<length-1;i ){
list[i]=list[i 1];
}
list[length]=null;
length--;
}
}
public DataElement retrieveAt(int location){
if(location<0||location>=length){
System.err.println("The location of item to be retrieved is out of range!!");
return null;
}
else{
return list[location].getCopy();
}
}
public void replacAt(int location,DataElement repItem){
if(location<0||location>=length)
System.out.println("The position of item to be replaced is out of range!!");
else
list[location]=repItem.getCopy();
}
public void clearList(){
for(int i=0;i<length;i ){
list[i]=null;
}
length=0;
System.gc();
}
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!!");
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




