A brief description of linear table

Source: Internet
Author: User

First, the simple realization increases, deletes, changes, checks
 Packagedatatructs;/*** Table Interface*/ Public InterfaceLinearlist {BooleanIsEmpty ();//determine if a linear table is empty    intSize ();//returns the size of a linear tableObject Get (intindex);//gets the element of the specified indexObject Set (intIndex,object element);//modifies the element of the specified index        BooleanAddforindex (intIndex,object element);//elements added at the specified index location        BooleanAdd (Object Element);//adding elements at the end of a linear tableObject Remove (intindex);//removes the element at the specified position        voidClear ();//Empty the linear table                }

Implementation class

 Packagedatatructs; Public classLinearlistimplImplementsLinearlist {//Linear Table    PrivateObject [] sList; //Linear Table Size    Private intsize;  PublicLinearlistimpl (intlength) {        if(length<0) {sList=NewObject[10]; }Else{sList=NewObject[length]; }    }             PublicLinearlistimpl () {//set Default to 10 sizes         This(10); }    /** Check if the linear table is empty*/@Override Public BooleanIsEmpty () {//False if size=0 indicates null true, not 0        returnSize==0; }    /** Size of the return line required table*/@Override Public intsize () {returnsize; }        /** Returns the element at the specified index position*/@Override PublicObject Get (intindex) {                returnSlist[index]; } @Override PublicObject Set (intindex, Object Element) {        //get the elements from the original positionObject old=Slist[index]; //Modifyslist[index]=element; //return the original value        returnOld ; }    /** Check whether the index is in range if the element is added at the specified location*/     Public voidCheckindexforadd (intindex) {        if(index<0 | | index>size) {            Throw NewIndexoutofboundsexception ("The index to be inserted is not in the range of the table"); }    }    /** Check if the specified index is within range*/     Public voidCheckIndex (intindex) {        if(index>size) {            Throw NewIndexoutofboundsexception ("The index to manipulate is not in the scope of the table"); }    }            /** Add at specified index location*/@Override Public BooleanAddforindex (intindex, Object Element)                {Checkindexforadd (index); //determine if there is room for a linear table        if(Size () = =slist.length) {            //if the 0            if(slist.length==0){                //initialized to tenslist=NewObject[10]; }Else{                //not empty on +1//Temp TableObject [] tmp=sList; //re +1                 This. slist=NewObject[slist.length+1]; //copy the elements.                 for(inti = 0; i < size; i++) {Slist[i]=Tmp[i]; }            }        }        //Move backward One         for(inti=size-1;i>=index;i--) {Slist[i]=slist[i+1]; }        //inserting elementsslist[index]=element; Size++; return true; }    /** Add elements at the end*/@Override Public BooleanAdd (Object element) {//call the Add method above         returnAddforindex (size,element); }    /** Delete Elements of the specified index*/@Override PublicObject Remove (intindex)        {CheckIndex (index);  for(inti=index;i<size-1;i++) {Slist[i]=slist[i+1]; } slist[--size]=NULL; //return the element to remove        returnSlist[index]; } @Override Public voidClear () { for(inti = 0; i < size; i++) {            //set each value to nullslist[i]=NULL; }        //set the linear table size to 0Size=0; }}

Test

 Packagedatatructs; Public classDemo {/**     * @paramargs*/     Public Static voidMain (string[] args) {Linearlistimpl ll=NewLinearlistimpl (); System.out.println ("is empty:" +ll.isempty ()); System.out.println ("Size:" +ll.size ()); Ll.add ("Zhang San"); Ll.add ("John Doe"); Ll.addforindex (2, "Harry"); System.out.println (Ll.set (2, "Zhao Liu")); Ll.remove (2);  for(inti = 0; I < ll.size (); i++) {System.out.print ("\ T" +i+ "element:" +Ll.get (i));        } ll.clear (); System.out.println ("Element size:" +ll.size ()); System.out.println ("is empty:" +ll.isempty ()); }}

A brief description of linear table

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.