ºìÁªLinuxÃÅ»§
Linux°ïÖú

JavaÊý¾Ý½á¹¹---»ùÓÚÊý×éµÄ±í

·¢²¼Ê±¼ä:2006-10-13 00:42:26À´Ô´:ºìÁª×÷Õß:×ÏÇí
ÎÒû¿´¹ý ÆäËûÓïÑÔ°æµÄÊý¾Ý½á¹¹,µ«¾õµÃjavaµÄʵÏÖ·½·¨ºÜÇÉÃî--ÓÃÀàºÍ¶ÔÏóÀ´ÊµÏÖ.»ùÓÚÊý×éµÄ±í,˼ÏëºÜ¼òµ¥¾ÍÊǶ¨ÒåÒ»¸öÀàÓÃÀ´´æ´¢Ò»×éÊý¾Ý,ÎÒ¶¨ÒåµÄÊÇArrayListClassÀà,ÔÚÀàÖж¨ÒåÓÃÀ´²Ù×÷Êý×éµÄ·½·¨.Æäʵ¾ÍÊÇ Õâô¼òµ¥,µ«¾ßÌå²Ù×÷ÆðÀ´¾Í»áÓöµ½ºÜ¶àÂé·³ÁË!

ÎÒÃÇÕâ¸öArrayListClassÀàÖÐÊ×ÏÈÓ¦¸Ã°üÀ¨Ò»¸öÊý×éÐ͵ÄÓòlist,ÓÃÀ´´æ·ÅÊý¾Ý,ÕâÑù·ÅÔÚͬһÊý×éÖÐÊý¾ÝÖ®¼ä¾Í²úÉúÁËλÖÃÉϵÄÁªÏµ,ʹ¶ÔÊý¾ÝµÄ²Ù×÷±ãµÄ¼òµ¥.È»¶øÕâ¸öÊý×éµ½µ×ÊÇʲôÊý¾ÝÀàÐ͵Ä,ÎÒÃÇÆÚÍûÕâ¸ö±íÄÜÓÃÓÚËùÓеÄÊý¾ÝÀàÐÍ,ÎÒÃDz»Äܽ«Ëûµ¥´¿µÄ¹Ì¶¨³ÉijһÖÖ.ËùÒÔÎÒÃDZØÐ뽫Õâ¸öÊý¾ÝÆÕͨ»¯,½â¾öµÄ°ì·¨¾ÍÊǶ¨ÒåÒ»¸öÀà,×÷ΪËùÓÐÊý¾ÝÀàÐ͵ij¬Àà.¿´Õâ¸ö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[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 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 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 list[i]=null;

}

length=0;

System.gc();

}


public void copyList(ArrayListClass otherList){

if(this!=otherList){

for(int i=0;i list[i]=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!!");

}


}


}

¾ÍÊÇÕâô¼òµ¥!!ÏàÐÅ˳Ðò±íÒ²¿ÉÒÔÇáËɸ߶¥ÁË.
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ