I have never seen other language version of the data structure, but I feel that Java's implementation is very clever-use class and object. Based on array table, the idea is very simple to define a class to store a group of data, I define Is the ArrayListClass class, defined in the class to operate an array method. It is actually as simple, but it will encounter a lot of trouble in the specific operation! We should first include an array-type domain List in our arrayListClass class. Data, which is placed between the data in the same array, which generates a location, which makes it easy to operate the data. However, this array is of the data type, we expect this page to be used for all data types, We cannot fix him simply into a certain kind. So we must put this data to be normalized, and the solution is to define a class as a superclass for all data types. Look at this 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 ();} he defined to abstract, then inherit and implement it in the definition of other data types, I define two types of data and 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 OtHelement) {Num = Othelement.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 Othelelement) {// Todo Auto-generated method stub intelement newe = (intelement) othelement
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 = ( StringeElement; return (str == newe.str);} / * (non-javadoc) * @see DataElement # Compareto (DataElement) * / public int COMPARETO (DataElement Otherel ement) {// 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 stringeElement OtHOD stringeElement (); {Return Str;} public string toString () {Return Str;}
}} The data type has been defined, so the data type of List can be defined as DATEELEMENT [], so you can include, so you want, as long as you define a DataElement child class, This is the essence of Java inheritance. We will then define the ArrayListClass class: protected int length; protected int maxSize; protected DataElement [] list; this is all of its domains. Next is its method, our operations There are many kinds, such as insertion, query, deletion, etc., we must implement them one by one, the specific method is no longer described again, and the last completion code public abstract class arraylistclass {// fields protected int length; protected int mapize; protected int mapize; protected [] 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
T INSERT INSTER THE THE INSTER = I - I-1];} list [location] = I-1];} list [location] = INSERTITEM.GETCOPY = INSERTITEM.GETCOPY (); Length ;}} public void insertInd (DataElement InsertItem) {if (Length> = maxsize) {system.err.println ("Can't Insert In a full1 !!");} else {list [ignition] = INSERTITEM.GETCOPY (); Length ;}} public void removeat {if (location <0 || location> = length) {system.err.println ("The location you want to remove is out of range! ! ");} El = location; i otherList.list [j] .getCopy ();}} public abstract int seqSearch (DataElement seqItem); public abstract void insert (DataElement insertItem); public abstract void remove (DataElement removeItem);} finally see the code you found the back The class is actually an abstract class. Why do you definite this? This is to do this for different types: sequence table and non-sequence table. It is not difficult to imagine that some of them are different, first look at the non-sequence table: 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; booleanfact = false; for (LOC = 0; LOC