Java Data Structure-array-based tables-general Linux technology-Linux programming and kernel information. For details, see the following. I have never read the data structures of other language versions, but I think java implementation methods are clever-using classes and objects for implementation. an array-based table defines a class to store a group of data. I define the ArrayListClass class and the method to operate arrays in the class. this is actually so simple, but the specific operation will encounter a lot of trouble!
In our ArrayListClass class, we should first include an array-type domain list to store data. In this way, data in the same Array produces a positional relationship, data operations are easy. however, what data type is this array? We expect this table to be used for all data types. We cannot fix it simply into a certain type. therefore, we must make this data common. The solution is to define a class as a superclass for all data types. check 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 ();
}
It is defined as abstract and inherited and implemented when other data types are defined. 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;
}
/* (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 ){
The data type has been defined, so the list data type can be defined as DateElement [], so that you can include what you want, as long as you define a subclass of DataElement when using it, this is exactly the essence of java inheritance. we then define the ArrayListClass class:
Protected int length;
Protected int maxSize;
Protected DataElement [] list; this is all its fields.
The next step is its method. There should be many types of table operations, such as insertion, query, and deletion. We need to implement them one by one. The specific method will not be described in detail, and we will 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 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 );
}
At the end of the code, you can find that this class is actually an abstract class. Why should we define it like this? The reason for this is to apply to different types: ordered tables and non-ordered tables. It is not hard to imagine that some of their methods are different. First, let's look at the non-ordered tables:
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 !! ");
}
}
}
That's easy !! I believe that the sequence table can be easily topped up.
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.