Java数据结构---基于数组的表

我没看过其他语言版的数据结构,但觉得java的实现方法很巧妙--用类和对象来实现.基于数组的表,思想很简单就是定义一个类用来存储一组数据,我定义的是ArrayListClass类,在类中定义用来操作数组的方法.其实就是这么简单,但具体操作起来就会遇到很多麻烦了!
我们这个ArrayListClass类中首先应该包括一个数组型的域list,用来存放数据,这样放在同一数组中数据之间就产生了位置上的联系,使对数据的操作便的简单.然而这个数组到底是什么数据类型的,我们期望这个表能用于所有的数据类型,我们不能将他单纯的固定成某一种.所以我们必须将这个数据普通化,解决的办法就是定义一个类,作为所有数据类型的超类.看这个DataElement:
publicabstractclassDataElement{
publicabstractbooleanequals(DataElementotherElement);
publicabstractintcompareTo(DataElementotherElement);
publicabstractvoidmakeCopy(DataElementotherElement);
publicabstractDataElementgetCopy();
}

将他定义成为抽象的,再在定义其他数据类型时继承并实现它,我定义了两个数据类型IntElement和StringElement:

IntElement:

publicclassIntElementextendsDataElement{
protectedintnum;

//constructors
publicIntElement(){
num=0;
}
publicIntElement(intnumber){
num=number;
}
publicIntElement(IntElementotherElement){
num=otherElement.num;
}

///get-setMethods
publicvoidsetNum(intnumber){
num=number;
}
publicintgetNum(){
returnnum;
}


/*(non-Javadoc)
*@seeDataElement#equals(DataElement)
*/
publicbooleanequals(DataElementotherElement){
//TODOAuto-generatedmethodstub
IntElementnewe=(IntElement)otherElement;
return(this.num==newe.num);
}

/*(non-Javadoc)
*@seeDataElement#compareTo(DataElement)
*/
publicintcompareTo(DataElementotherElement){
//TODOAuto-generatedmethodstub
IntElementnewe=(IntElement)otherElement;
if(this.num==newe.num)
return0;
elseif(this.num>newe.num)
return1;
else
return-1;
}

/*(non-Javadoc)
*@seeDataElement#makeCopy(DataElement)
*/
publicvoidmakeCopy(DataElementotherElement){
//TODOAuto-generatedmethodstub
IntElementnewe=(IntElement)otherElement;
this.num=newe.num;

}

/*(non-Javadoc)
*@seeDataElement#getCopy()
*/
publicDataElementgetCopy(){
//TODOAuto-generatedmethodstub
IntElementnewElement=newIntElement();
newElement.num=this.num;
returnnewElement;
}
publicStringtoString(){
returnString.valueOf(num);
}
}

StringElement:

publicclassStringElementextendsDataElement{

/**
*
*/
privateStringstr;

//constructors
publicStringElement(){
str=null;

}
publicStringElement(Stringstring){
str=string;
}
publicStringElement(StringElementotherElement){
str=otherElement.str;
}

//get-setMethods
publicvoidsetStr(Stringstring){
str=string;
}
publicStringgetStr(){
returnstr;
}

/*(non-Javadoc)
*@seeDataElement#equals(DataElement)
*/
publicbooleanequals(DataElementotherElement){
//TODOAuto-generatedmethodstub
StringElementnewe=(StringElement)otherElement;
return(str==newe.str);
}

/*(non-Javadoc)
*@seeDataElement#compareTo(DataElement)
*/
publicintcompareTo(DataElementotherElement){
//TODOAuto-generatedmethodstub
StringElementnewe=(StringElement)otherElement;


return(str.compareTo(newe.str));
}

/*(non-Javadoc)
*@seeDataElement#makeCopy(DataElement)
*/
publicvoidmakeCopy(DataElementotherElement){
//TODOAuto-generatedmethodstub
StringElementnewe=(StringElement)otherElement;
str=newe.str;
}

/*(non-Javadoc)
*@seeDataElement#getCopy()
*/
publicDataElementgetCopy(){
//TODOAuto-generatedmethodstub

StringElementothere=newStringElement();
othere.str=str;
returnothere;

}

publicStringtoString(){
returnstr;
}
}

已经定义好了数据类型,所以list的数据类型我们就可以定义为DateElement[]了,这样就可以包括所以你想要的了,只要你在用的时候定义一个DataElement的子类就行了,这正是java继承的精髓所在.我们接着定义ArrayListClass类:

protectedintlength;
protectedintmaxSize;
protectedDataElement[]list;这就是它的所有域了.

接下来就是它的方法了,我们对表的操作应该有很多种,比如插入、查询、删减等等,我们要逐个的实现,具体方法不再赘述,且看最后完成代码

publicabstractclassArrayListClass{
//fields
protectedintlength;
protectedintmaxSize;
protectedDataElement[]list;

//defaltconstructors
publicArrayListClass(){
length=0;
maxSize=100;
list=newDataElement[maxSize];
}
//constructors
publicArrayListClass(intsize){
if(size<=0){
System.err.println("Thearrysizemustbepositive.Creatinganarrayofsize100.");
maxSize=100;
}
else
maxSize=size;
length=0;
list=newDataElement[maxSize];
}
publicArrayListClass(ArrayListClassotherList){
maxSize=otherList.maxSize;
length=otherList.length;
list=newDataElement[maxSize];
for(inti=0;i<length;i++){
list[i]=otherList.list[i].getCopy();
}
}

//methods
publicbooleanisEmpty(){
return(length==0);
}
publicbooleanisFull(){
return(length==maxSize);
}
publicintlistSize(){
returnlength;
}
publicintmaxListSize(){
returnmaxSize;
}
publicvoidprint(){
for(inti=0;i<length;i++){
System.out.print(list[i]+"");
}
System.out.println();
}
publicbooleanisItemAtEqual(intlocation,DataElementitem){
return(list[location].equals(item));
}
publicvoidinsrtAt(intlocation,DataElementinsertItem){
if(location<0||location>+maxSize){
System.out.println("Thepositionoftheitemtobeinsertedisoutofrange!!");
}
else
if(length>=maxSize)
System.err.println("Can'tinsertinafulllist!!");
else{
for(inti=length;i>location;i--){
list[i]=list[i-1];
}
list[location]=insertItem.getCopy();
length++;
}
}
publicvoidinsertEnd(DataElementinsertItem){
if(length>=maxSize){
System.err.println("Can'tinsertinafulllist!!");
}

else{
list[length]=insertItem.getCopy();
length++;
}
}
publicvoidremoveAt(intlocation){
if(location<0||location>=length){
System.err.println("Thelocationyouwanttoremoveisoutofrange!!");
}
else{
for(inti=location;i<length-1;i++){
list[i]=list[i+1];
}
list[length]=null;
length--;
}
}
publicDataElementretrieveAt(intlocation){
if(location<0||location>=length){
System.err.println("Thelocationofitemtoberetrievedisoutofrange!!");
returnnull;
}
else{
returnlist[location].getCopy();
}
}
publicvoidreplacAt(intlocation,DataElementrepItem){
if(location<0||location>=length)
System.out.println("Thepositionofitemtobereplacedisoutofrange!!");
else
list[location]=repItem.getCopy();
}
publicvoidclearList(){
for(inti=0;i<length;i++){
list[i]=null;
}
length=0;
System.gc();
}
publicvoidcopyList(ArrayListClassotherList){
if(this!=otherList){
for(inti=0;i<length;i++)
list[i]=null;
System.gc();
maxSize=otherList.maxSize;
length=otherList.length;
list=newDataElement[maxSize];

for(intj=0;j<length;j++)
list[j]=otherList.list[j].getCopy();
}
}
publicabstractintseqSearch(DataElementseqItem);
publicabstractvoidinsert(DataElementinsertItem);
publicabstractvoidremove(DataElementremoveItem);
}
看到代码的最后你回发现这个类其实是一个抽象类,为什么要这样定义呢?之所以这样我们是为了针对不同是类型:顺序表和非顺序表.不难想象他们的一些方法是存在差异的,先看一下非顺序表:

publicclassUnorderedArrayListextendsArrayListClass{

/**
*
*/
publicUnorderedArrayList(){
super();
//TODOAuto-generatedconstructorstub
}

/*(non-Javadoc)
*@seeArrayListClass#seqSearch(DataElement)
*/
publicintseqSearch(DataElementseqItem){
//TODOAuto-generatedmethodstub
intloc;
booleanfound=false;

for(loc=0;loc<length;loc++)
if(list[loc].equals(seqItem))
{
found=true;
break;
}
if(found)
returnloc;
else
return-1;
}

/*(non-Javadoc)
*@seeArrayListClass#insert(DataElement)
*/
publicvoidinsert(DataElementinsertItem){
//TODOAuto-generatedmethodstub
intloc;
if(length==0)
list[length++]=insertItem.getCopy();
else
if(length==maxSize)
System.err.println("Can'tinsertinafulllist!!");
else{
loc=seqSearch(insertItem);

if(loc==-1)
list[length++]=insertItem.getCopy();
else
System.err.println("Theitemtobeinsertedisallreadyinthelist!!");

}
}

/*(non-Javadoc)
*@seeArrayListClass#remove(DataElement)
*/
publicvoidremove(DataElementremoveItem){
//TODOAuto-generatedmethodstub
intloc;

if(length==0)
System.err.println("Can'tdeletefromaemptylist!!");
else{
loc=seqSearch(removeItem);
if(loc!=-1)
removeAt(loc);
else
System.err.println("Theitemtobedeletedisnotinthelist!!");
}

}

}

就是这么简单!!相信顺序表也可以轻松高顶了.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值