Java Implementation Order table structure

Source: Internet
Author: User

Package COM.HEPHEC;

Import Java.util.Arrays;

Public class sequencelist<t>{

Creates a sequential linear table with an array of specified lengths
//@paramElement Specifies the first element in a sequential linear table
//@paramInitsize the length of the underlying array of the specified sequential linear table
private int default_size = 16;
Save the length of the array
private int capacity;
Defines an array to hold elements of a sequential linear table
Private object[] Elementdata;
Save the current number of elements in a sequential linear table
private int size=0;
Create a null-order linear table with the default array length
Public Sequencelist () {
Capacity=default_size;
Elementdata=new Object[capacity];
}
Public sequencelist (T element) {
This ();
Elementdata[0]=element;
size++;
}
Public Sequencelist (T element,int initsize) {
Capacity=1;
Set the capacity to a minimum of 2 of the n-th square of the initsize
while (capacity < Initsize) {
Capacity <<= 1;
}
Elementdata=new Object[capacity];
Elementdata[0]=element;
size++;
}
Get the size of a sequential linear table
public int Length () {
return size;
}

Public T get (int i) {
if (i<0| | I&GT;SIZE-1) {
throw new Indexoutofboundsexception ("Array table index out of Bounds");
}
Return (T) elementdata[i];
}
Finding the index of a specified element in a sequential linear table
public int Locate (T element) {
for (int i=0;i<size;i++) {
if (elementdata[i].equals (element)) {
return i;
}
}return-1;
}
Inserts an element into the specified position of the sequential table
public void Insert (T Element,int index) {
if (index<0| | Index>size) {
throw new Indexoutofboundsexception ("Sequential linear table index out of Bounds");
}

Move all subsequent elements of index one cell backward
Ensurecapacity (size+1);
Move all elements in the future at index one cell backward
System.arraycopy (Elementdata, Index, Elementdata, index+1, Size-index);
Elementdata[index]=element;
size++;
}
Adds an element to the start of a linear order table
public void Add (T element) {
Insert (element,size);
}

Extend the top-level array length
private void ensurecapacity (int mincapacity) {
If the original length of the array is less than the current required length
if (mincapacity>capacity) {
Continue to capacity*2 until capacity is greater than mincapacity
while (capacity<mincapacity) {
capacity<<=1;
}
Elementdata=arrays.copyof (elementdata,capacity);

}
}
Delete the element at the specified index in the order table
Public T Delete (int index) {
if (index<0| | index>size+1) {
throw new Indexoutofboundsexception ("linear table index out of Bounds");
}
T oldvalue= (t) Elementdata[index];
int nummoved=size-index-1;
if (nummoved>0) {
System.arraycopy (Elementdata, index+1, Elementdata, Index, nummoved);

}
Empty the last element
Elementdata[--size]=null;
return oldValue;
}
Delete the last element in a sequential linear table
Public T Remove () {
return Delete (size-1);
}
Determine if the order table is an empty table
public Boolean isEmpty () {
return size==0;
}
Empty the linear table
public void Clear () {
Assigns all elements of the underlying array to null
Arrays.fill (elementdata, NULL);
size=0;
}
Public String toString () {
if (size==0) {
Return "[]";
}
else{
StringBuilder sb=new StringBuilder ("[");
for (int i=0;i<size;i++) {
Sb.append (elementdata[i].tostring () + ",");
}
int Len=sb.length ();
Return Sb.delete (Len-2, Len). Append ("]"). ToString ();
}

}

}

Java Implementation Order table structure

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.