Use JAVA arrays to implement sequence tables and java array sequence tables

Source: Internet
Author: User

Use JAVA arrays to implement sequence tables and java array sequence tables

1. JAVA generic classes are introduced. Therefore, an array of Object [] types is defined to save various types of objects.

2. The default constructor creates an Object array with a default size of 16. The constructor with parameters creates an Object array with a specified length.

3. Basic operations for ordered tables include: return the table length and obtain the elements at the specified index (note that it is an index, not a location. Index start from 0 and start from 1), search for the location of data elements by value, and insert elements directly (end of the sequence table) insert an element to the specified position, directly Delete the element (at the end of the sequence table), delete the element at the specified index, determine whether the table is empty, and clear the table.

4. In the Java class library, the java. util. ArrayList class implements ordered tables. Therefore, you can directly use the ArrayList in the JAVA class library to perform operations on ordered tables. The code for implementing the sequence table is as follows:

1 import java. util. Arrays; 2 3 public class SequenceList <T> {4 private final int DEFAULT_SIZE = 16; // the final instance variable displays the specified initial value and does not change. 5 6 private Object [] elementData; // This array is used to save the 7 private int capacity element in the sequence table; // The length of the array is 8 private int size; // Save the number of current elements in the sequence table 9 10 // create the sequence table 11 public SequenceList () {12 capacity = DEFAULT_SIZE; 13 elementData = new Object [capacity]; 14} 15 16 // create a sequence table 17 public SequenceList (int initSize) {18 capacity = 1; 19 while (capacity <initSize) 20 capacity <= 1; // set capacity to a minimum of 2 to 21 elementD of initSize Ata = new Object [capacity]; 22} 23 24 // obtain the number of current elements in the sequence table 25 public int length () {26 return size; 27} 28 29 // obtain the element where the index in the sequence table is I. I indicates the index, which is 30 public T get (int I) starting from 0) {31 if (I <0 | I> size-1) 32 throw new IndexOutOfBoundsException ("sequential table Index out of bounds"); 33 return (T) elementData [I]; 34} 35 36 // view the index of the specified element in the sequence table. If not,-1 37 public int locate (T element) {38 for (int I = 0; I <size; I ++) 39 if (elementData [I]. equa Ls (element) 40 return I; 41 return-1; 42} 43 44 // insert an element 45 public void insert (T element, int index) {46 if (index <0 | index> size) 47 throw new IndexOutOfBoundsException ("sequential table index out of bounds"); 48 ensureCapacity (size + 1 ); // make sure that the sequence table is expanded when it is full, so that element 49 can be inserted. // move all elements after the specified index to a position of 50 // System. arraycopy (elementData, index, elementData, index + 1, size-index); 51 for (int I = size; I> index; I --) 52 elem EntData [I] = elementData [I-1]; 53 elementData [index] = element; 54 size ++; // increase the number of elements in the sequence table by 1 55} 56 57 private void ensureCapacity (int minCapacity) {58 // resize the array when the array capacity is full. Extend the capacity to the power of 59 if (minCapacity> capacity) {60 while (capacity <minCapacity) 61 capacity <= 1; 62 elementData = Arrays. copyOf (elementData, capacity); 63} 64} 65 66 // add an element 67 public void add (T element) {68 insert (element, size) at the end of the sequence table ); 69} 70 71 // delete the element 72 public T delete (int index) at the specified index in the sequence table {73 if (index <0 | index> size-1) 74 throw new IndexOutOfBoundsException ("sequential table index out of bounds"); 75 T oldValue = (T) elementData [index]; 76 int numMoved = size-index-1; // calculate the number of elements to be moved 77 if (numMoved> 0) {78 System. arraycopy (elementData, index + 1, elementData, index, numMoved); 79} 80 elementData [-- size] = null; // Let the Garbage Collector recycle it in time, avoid Memory leakage 81 return oldValue; 82} 83 84 // delete the last element of the sequence table 85 public T remove () {86 return delete (size-1 ); 87} 88 89 // judge whether the sequence table is empty. Table 90 public boolean empty () {91 return size = 0; 92} 93 94 // clear the order Table 95 public void clear () {96 Arrays. fill (elementData, null); // assign null 97 size = 0 to each element in the array elementData; 98} 99 100 public String toString () {101 if (size = 0) 102 return "[]"; 103 else {104 StringBuilder sb = new StringBuilder ("["); 105 for (int I = 0; I <size; I ++) 106 sb. append (elementData [I]. toString () + ","); 107 int len = sb. length (); 108 // Delete the two extra characters (one is a comma and the other is a space) added at the end of the for Loop above) 109 return sb. delete (len-2, len ). append ("]"). toString (); 110} 111} 112}

 

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.