In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to implement array-based tables in Java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
I haven't seen the data structures in other languages, but I think the implementation of java is very clever-using classes and objects. Based on the array table, the idea is simply to define a class to store a set of data. I define the ArrayListClass class and define the methods used to manipulate the array in the class. In fact, it is that simple, but the specific operation will encounter a lot of trouble!
First of all, our ArrayListClass class should include an array field list, which is used to store data, so that there is a position relationship between the data in the same array, which makes it easy to operate the data. However, what is the data type of this array? we expect this table to be used for all data types, and we can't simply fix it into one. So we have to generalize this data, 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 ();}
By defining it as abstract and inheriting and implementing it when defining other data types, I defined two data types, IntElement and 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;}}
The data type has been defined, so we can define the data type of list as DateElement [], so we can include what you want, as long as you define a subclass of DataElement when you use it, which is the essence of java inheritance. Let's go on to define the ArrayListClass class:
Protected int length; protected int maxSize; protected DataElement [] list; this is all its domains.
Next is its method, we should have many kinds of operations on the table, such as insert, query, delete, etc., we have to implement one by one, the specific method will not be detailed, let's see the final completion of the code.
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=maxSize) System.err.println ("Can't insert in a full listings!"); else {for (int ilistings) {list= listings [I-1];} list [location] = insertItem.getCopy (); length++ } public void insertEnd (DataElement insertItem) {if (length > = maxSize) {System.err.println ("Can't insert in a full listings!");} else {list [length] = insertItem.getCopy (); length++;}} public void removeAt (int location) {if (location=length) {System.err.println ("The location you want to remove is out of range!");} else {for (int Illustrated locationshifts list=list [item1];} list [length] = null; length-- } public DataElement retrieveAt (int location) {if (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=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 itemo political list=null;} length=0) System.gc ();} public void copyList (ArrayListClass otherList) {if (thisometric otherList) {for (int item0 for I list=null; System.gc (); maxSize=otherList.maxSize; length=otherList.length; list=new DataElement [maxSize]; for (int jung0 for j list [j] = otherList.list.getCopy ();}} public abstract int seqSearch (DataElement seqItem); public abstract void insert (DataElement insertItem); public abstract void remove (DataElement removeItem);}
When you see the end of the code, you find that this class is actually an abstract class. Why do you define it this way? The reason for this is for different types: sequential tables and non-sequential tables. It's not hard to imagine that some of their methods are different. Take a look at the non-sequence table first:
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 listings!"); else {loc=seqSearch (insertItem); if (loc==-1) list [length++] = insertItem.getCopy (); else System.err.println ("The item to be inserted is allready in the listings!") }} / * (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 an empty listings!"); else {loc=seqSearch (removeItem); if (localized loc=seqSearch 1) removeAt (loc); else System.err.println ("The item to be deleted is not in the listings!") } this is the end of the article on "how to implement array-based tables in Java". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.