Java資料結構---基於陣列的表(轉)
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!!");
}
}
}
就是這麼簡單!!相信順序表也可以輕鬆高頂了.
我們這個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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java資料結構-陣列Java資料結構陣列
- Java版-資料結構-陣列Java資料結構陣列
- 基礎資料結構之陣列資料結構陣列
- 玩轉資料結構之陣列資料結構陣列
- Java版-資料結構-佇列(陣列佇列)Java資料結構佇列陣列
- 資料結構-陣列資料結構陣列
- 資料結構 - 陣列資料結構陣列
- (一)Java資料結構之稀疏陣列Java資料結構陣列
- java資料結構學習之陣列Java資料結構陣列
- PHP 陣列轉樹結構/樹結構轉陣列PHP陣列
- 資料結構之「陣列」資料結構陣列
- 資料結構之陣列資料結構陣列
- 資料結構2——陣列資料結構陣列
- Rust中陣列資料結構基礎知識Rust陣列資料結構
- java 資料結構實現陣列封裝 (一)Java資料結構陣列封裝
- JavaScript資料結構01 - 陣列JavaScript資料結構陣列
- JS資料結構(一)——陣列JS資料結構陣列
- 資料結構——樹狀陣列資料結構陣列
- Java中的陣列資料結構需要了解的要點Java陣列資料結構
- 資料結構基礎學習之(串與陣列)資料結構陣列
- 慕課網玩轉資料結構課程之陣列資料結構陣列
- js實現資料結構--陣列JS資料結構陣列
- JavaScript資料結構之陣列棧佇列JavaScript資料結構陣列佇列
- JavaScript模擬表單(帶陣列的複雜資料結構)提交JavaScript陣列資料結構
- js 中基礎資料結構陣列去重問題JS資料結構陣列
- 資料結構之陣列和連結串列資料結構陣列
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 看圖輕鬆理解資料結構與演算法系列(基於陣列的棧)資料結構演算法陣列
- 工具函式:普通陣列如何轉為樹形結構資料(多層級)陣列?函式陣列
- 資料結構與演算法——陣列資料結構演算法陣列
- LeetCode之資料結構——陣列LeetCode資料結構陣列
- 資料結構之php實現陣列資料結構PHP陣列
- 資料結構之連結串列與陣列(1):陣列和連結串列的簡介資料結構陣列
- ES6的Set、Map資料結構 陣列資料結構陣列
- 陣列結構之陣列陣列
- 資料結構之陣列和矩陣--矩陣&不規則二維陣列資料結構陣列矩陣
- 重溫四大基礎資料結構:陣列、連結串列、佇列和棧資料結構陣列佇列
- 【資料結構與演算法】——稀疏陣列資料結構演算法陣列