Data | structure | Array I have not seen the data structures in other languages, but I think Java implementations are ingenious--with classes and objects. An array of tables, the idea is very simple is to define a class to store a set of data, I define the Arraylistclass class, Defines the method used to manipulate an array in a class. In fact, it is so simple, but the concrete operation will encounter a lot of trouble!
Our Arraylistclass class should first include an array-type list of fields to hold the data so that the position is connected between the data in the same array, making the operation of the data simple. But what kind of data is this array? We expect the table to be used for all types of data, and we can't just fix it. So we have to make this data generic, and the solution is to define a class as a superclass of 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 ();
}
To define him as abstract, and then inherit and implement it when defining other data types, I have 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;
}
The data type has been defined, so the data type of the list can be defined as dateelement[] so that you can include what you want, as long as you define a dataelement subclass when you use it, This is the essence of Java inheritance. We then define the Arraylistclass class:
protected int length;
protected int maxSize;
protected dataelement[] list; This is all the fields of it.
The next is its method, we should have a lot of the operation of the table, such as inserting, query, deletion, and so on, we have to implement one by one, the specific method no longer repeat, and see the final completion 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<=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" the item to being 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 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 the item to being 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 the item to being 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);
}
When you see the end of the code, you find out that this class is actually an abstract class, why do you define it this way? This is what we do for different types: sequential and non sequential tables. It is not difficult to imagine some of their methods are different, first look at the non-sequential 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;
Boolean found=false;
for (loc=0;loc<length;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 are 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 isn't in" list!!);
}
}
}
It's so simple!! I believe that the order table can also be easily high.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.