Linear table description, linear description

Source: Internet
Author: User

Linear table description, linear description
1. Simple addition, deletion, modification, and query

Package datatructs;/*** Table interface */public interface LinearList {boolean isEmpty (); // judge whether the linear table is empty int size (); // returns the size of the linear table Object get (int index); // obtains the Object set (int index, Object element) of the specified index ); // modify the specified index element boolean addForIndex (int index, Object element); // add the element boolean add (Object element) at the specified index location ); // Add the Element Object remove (int index) at the end of the linear table; // remove the element void clear () at the specified position; // clear the linear table}

Implementation class

Package datatructs; public class LinearListImpl implements LinearList {// linear table private Object [] sList; // linear table size private int size; public LinearListImpl (int length) {if (length <0) {sList = new Object [10];} else {sList = new Object [length] ;}} public LinearListImpl () {// set the default value to 10 this (10);}/** check whether the linear table is empty */@ Override public boolean isEmpty () {// If size = 0, it indicates that it is null. If it is not 0, it is false return size = 0 ;} /** return the size of the required table */@ Override public int size () {return size ;} /** return the element at the specified index position */@ Override public Object get (int index) {return sList [index];} @ Override public Object set (int index, Object element) {// obtain the original position element Object old = sList [index]; // modify sList [index] = element; // return the original value return old ;} /** check whether the index is within the specified range when an element is added */public void checkIndexForAdd (int index) {if (index <0 | index> size) {throw new IndexOutOfBoundsException ("the index to be inserted is not in the table range");}/** check whether the specified index is in the range */public void checkIndex (int index) {if (index> size) {throw new IndexOutOfBoundsException ("the index to be operated is not in the table range ");}} /** add */@ Override public boolean addForIndex (int index, Object element) {checkIndexForAdd (index); // determine whether the linear table has space if (size () = sList. length) {// if it is 0 if (sList. length = 0) {// The Initialization is 10 sList = new Object [10];} else {// if it is not empty, + 1 // temporary table Object [] tmp = sList; // re-+ 1 this. sList = new Object [sList. length + 1]; // copy the element for (int I = 0; I <size; I ++) {sList [I] = tmp [I] ;}} // move one for (int I = size-1; I> = index; I --) {sList [I] = sList [I + 1];} // Insert the sList [index] = element; size ++; return true;}/** add the final element */@ Override public boolean add (Object element) {// call the above add method return addForIndex (size, element);}/** Delete the element of the specified index */@ Override public Object remove (int index) {checkIndex (index); for (int I = index; I <size-1; I ++) {sList [I] = sList [I + 1];} sList [-- size] = null; // return the element to be removed. return sList [index] ;}@ Override public void clear () {for (int I = 0; I <size; I ++) {// set each value to null sList [I] = null;} // set the linear table size to 0 size = 0 ;}}

 

Test

Package datatructs; public class Demo {/*** @ param args */public static void main (String [] args) {LinearListImpl ll = new LinearListImpl (); System. out. println ("null:" + ll. isEmpty (); System. out. println ("Size:" + ll. size (); ll. add ("Zhang San"); ll. add ("Li Si"); ll. addForIndex (2, "Wang Wu"); System. out. println (ll. set (2, "Zhao six"); ll. remove (2); for (int I = 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 ("null:" + ll. isEmpty ());}}

 

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.