資料|資料結構|數組 我沒看過 其他語言版的資料結構,但覺得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();
}
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;
}
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;
/* (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!!");
}