Java資料結構---基於陣列的表(轉)

ba發表於2007-08-16
Java資料結構---基於陣列的表(轉)[@more@]我沒看過 其他語言版的資料結構,但覺得java的實現方法很巧妙--用類和物件來實現.基於陣列的表,思想很簡單就是定義一個類用來儲存一組資料,我定義的是ArrayListClass類,在類中定義用來運算元組的方法.其實就是 這麼簡單,但具體操作起來就會遇到很多麻煩了!

我們這個ArrayListClass類中首先應該包括一個陣列型的域list,用來存放資料,這樣放在同一陣列中資料之間就產生了位置上的聯絡,使對資料的操作便的簡單.然而這個陣列到底是什麼資料型別的,我們期望這個表能用於所有的資料型別,我們不能將他單純的固定成某一種.所以我們必須將這個資料普通化,解決的辦法就是定義一個類,作為所有資料型別的超類.看這個DataElement:

public abstract class DataElement {

public abstract boolean equals(DataElement otherElement);

public abstract int compareTo(DataElement otherElement);

public abstract void makeCopy(DataElement otherElement);

public abstract DataElement getCopy();

}


將他定義成為抽象的,再在定義其他資料型別時繼承並實現它,我定義了兩個資料型別IntElement和StringElement:


IntElement:


public class IntElement extends DataElement {

protected int num;


//constructors

public IntElement(){

num=0;

}

public IntElement(int number){

num=number;

}

public IntElement(IntElement otherElement){

num=otherElement.num;

}


///get-set Methods

public void setNum(int number){

num=number;

}

public int getNum(){

return num;

}



/* (non-Javadoc)

* @see DataElement#equals(DataElement)

*/

public boolean equals(DataElement otherElement) {

// TODO Auto-generated method stub

IntElement newe=(IntElement)otherElement;

return (this.num==newe.num);

}


/* (non-Javadoc)

* @see DataElement#compareTo(DataElement)

*/

public int compareTo(DataElement otherElement) {

// TODO Auto-generated method stub

IntElement newe=(IntElement)otherElement;

if(this.num==newe.num)

return 0;

else if(this.num>newe.num)

return 1;

else

return -1;

}


/* (non-Javadoc)

* @see DataElement#makeCopy(DataElement)

*/

public void makeCopy(DataElement otherElement) {

// TODO Auto-generated method stub

IntElement newe=(IntElement)otherElement;

this.num=newe.num;


}


/* (non-Javadoc)

* @see DataElement#getCopy()

*/

public DataElement getCopy() {

// TODO Auto-generated method stub

IntElement newElement=new IntElement();

newElement.num=this.num;

return newElement;

}

public String toString(){

return String.valueOf(num);

}

}


StringElement:


public class StringElement extends DataElement {


/**

*

*/

private String str;


//constructors

public StringElement() {

str=null;


}

public StringElement(String string){

str=string;

}

public StringElement(StringElement otherElement){

str=otherElement.str;

}


//get-set Methods

public void setStr(String string){

str=string;

}

public String getStr(){

return str;

}


/* (non-Javadoc)

* @see DataElement#equals(DataElement)

*/

public boolean equals(DataElement otherElement) {

// TODO Auto-generated method stub

StringElement newe=(StringElement)otherElement;

return (str==newe.str);

}


/* (non-Javadoc)

* @see DataElement#compareTo(DataElement)

*/

public int compareTo(DataElement otherElement) {

// TODO Auto-generated method stub

StringElement newe=(StringElement)otherElement;


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

}


/* (non-Javadoc)

* @see DataElement#makeCopy(DataElement)

*/

public void makeCopy(DataElement otherElement) {

// TODO Auto-generated method stub

StringElement newe=(StringElement)otherElement;

str=newe.str;

}


/* (non-Javadoc)

* @see DataElement#getCopy()

*/

public DataElement getCopy() {

// TODO Auto-generated method stub


StringElement othere=new StringElement();

othere.str=str;

return othere;


}


public String toString(){

return str;

}

}


已經定義好了資料型別,所以list的資料型別我們就可以定義為DateElement[]了,這樣就可以包括所以你想要的了,只要你在用的時候定義一個DataElement的子類就行了,這正是java繼承的精髓所在.我們接著定義ArrayListClass類:


protected int length;

protected int maxSize;

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=otherList.list.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+" ");

}

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=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 list=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 list=null;

}

length=0;

System.gc();

}


public void copyList(ArrayListClass otherList){

if(this!=otherList){

for(int i=0;i list=null;

System.gc();

maxSize=otherList.maxSize;

length=otherList.length;

list=new DataElement[maxSize];


for(int j=0;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 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!!");

}


}


}

就是這麼簡單!!相信順序表也可以輕鬆高頂了.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617731/viewspace-960548/,如需轉載,請註明出處,否則將追究法律責任。

相關文章