Implementing a sequential table using a Java array

Source: Internet
Author: User

1, a Java generic class is introduced, so an array of object[] types is defined so that objects of various types can be saved.

2, the default construction method creates an object array with a default size of 16, and constructs a parameter to create an object array of the specified length

3, the basic operation of the implemented order table is to return the length of the table, get the element at the specified index (note the index, not the location. The index starts with the following 0, starting at position 1, locating the data element by value, inserting the element directly (the tail of the sequential table), inserting an element into the specified position, deleting the element directly (at the end of the sequential table), deleting the element at the specified index, and determining whether the table is empty or emptying the table.

1 Importjava.util.Arrays;2 3  Public classSequencelist<t> {4     Private Final intDefault_size = 16;//The final instance variable displays the specified initial value and no longer changes. 5     6     PrivateObject[] Elementdata;//the array is used to hold the elements in the sequential table7     Private intcapacity;//Save the length of the array8     Private intSize//Save the current number of elements in the order table9     Ten     //Create a sequential table with the default size One      Publicsequencelist () { ACapacity =default_size; -Elementdata =Newobject[capacity]; -     } the      -     //Create a sequential table with the specified size -      PublicSequencelist (intinitsize) { -Capacity = 1; +          while(Capacity <initsize) -Capacity <<= 1;//set capacity to a minimum of 2 times greater than initsize +Elementdata =Newobject[capacity]; A     } at      -     //gets the number of current elements in the order table -      Public intLength () { -         returnsize; -     } -      in     //gets the element that is indexed to I in the order table, I represents the index, which starts with 0 -      PublicT Get (inti) { to         if(I < 0 | | i > size-1) +             Throw NewIndexoutofboundsexception ("Sequential table index out of Bounds"); -         return(T) elementdata[i]; the     } *      $     //View the index of the specified element in the order table, return 1 if not foundPanax Notoginseng      Public intLocate (T element) { -          for(inti = 0; i < size; i++) the             if(Elementdata[i].equals (Element)) +                 returni; A         return-1; the     } +      -     //inserts an element at the specified index of the sequential table $      Public voidInsert (T element,intindex) { $         if(Index < 0 | | index >size) -             Throw NewIndexoutofboundsexception ("Sequential table index out of Bounds"); -Ensurecapacity (size + 1);//ensures that the sequential table is fully expandable to insert elements the         //moves all elements after the specified index one position backward - //system.arraycopy (elementdata, index, Elementdata, index + 1, size-index);Wuyi          for(inti = size; i > Index; i--) theElementdata[i] = elementdata[i-1]; -Elementdata[index] =element; Wusize++;//increase the number of elements in the sequential table by 1 -     } About      $     Private voidEnsurecapacity (intmincapacity) { -         //When the array capacity is full, the arrays are enlarged. Extend capacity to a minimum of 2 greater than mincapacity -         if(Mincapacity >capacity) { -              while(Capacity <mincapacity) ACapacity <<= 1; +Elementdata =arrays.copyof (elementdata, capacity); the         } -     } $      the     //add an element at the end of a sequential table the      Public voidAdd (T element) { the Insert (element, size); the     } -      in     //Delete the element at the specified index in the order table the      PublicT Delete (intindex) { the         if(Index < 0 | | index > SIZE-1) About             Throw NewIndexoutofboundsexception ("Sequential table index out of Bounds"); theT OldValue =(T) elementdata[index]; the         intnummoved = size-index-1;//count the number of elements that need to be moved the         if(nummoved > 0){ +System.arraycopy (elementdata, index + 1, Elementdata, Index, nummoved); -         } theElementdata[--size] =NULL;//keep the garbage collector in time to avoid memory leaksBayi         returnOldValue; the     } the      -     //Delete the last element in the order table -      PublicT Remove () { the         returnDelete (size-1); the     } the      the     //determine if the order table is an empty table -      Public Booleanempty () { the         returnSize = = 0; the     } the     94     //Empty Order table the      Public voidClear () { theArrays.fill (Elementdata,NULL);//assigns a value of NULL to each element in the array elementdata theSize = 0;98     } About      -      PublicString toString () {101         if(Size = = 0)102             return"[]";103         Else{104StringBuilder SB =NewStringBuilder ("["); the              for(inti = 0; i < size; i++)106Sb.append (elementdata[i].tostring () + ",");107             intLen =sb.length ();108             //remove the extra two characters (one is a comma, one is a space symbol) due to the last added in the For Loop109             returnSb.delete (Len-2, Len). Append ("]"). toString (); the         }111     } the}

Implementing a sequential table using a Java array

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.