Java Practice: A simple dynamic array implementation

Source: Internet
Author: User
A simple, dynamic array implementation

Adding 10w of capacity based on array implementations the average is 0.4 seconds to delete all the capacity this efficiency is considerable below to look at the code together

Package Com.array;import java.util.list;import java.util.random;/** * * @author Xiaotian * @date 2018-08-08 *///Implementation based on dynamic array E is generics//borrowed a bit of Java ArrayList Code//research source is also a pleasure//can also make our technology improved public class Arraylist<e> implements        java.io.serializable{/** * Initial capacity */private static final int default_capacity = 10;     /** * Shared empty array instance for empty instances.        */transient object[] empty_elementdata = {};     /** * Array buffer in which to store ArrayList elements.     * The capacity of the ArrayList is the length of this array buffer.        */transient object[] elementdata;     /** * Shared empty array instance for empty instances of the default size.        */private static final object[] Defaultcapacity_empty_elementdata = {};        /** * Size * */private int size;    /** * default is empty */public ArrayList () {this.elementdata = Defaultcapacity_empty_elementdata; }/** * Custom space Size * @param initialcapacity */public ArrayList (int initialcapacity) {if (Initi   Alcapacity > 0) {this.elementdata = new object[initialcapacity];     } else if (initialcapacity = = 0) {this.elementdata = Empty_elementdata;                                               } else {throw new illegalargumentexception ("Illegal capacity:" +        initialcapacity);    }} public int size () {return size;}    /** * Add an element element where the position is the last * @param e */public void Add (e e) {extendelement (size + 1);    this.elementdata[size++] = e;    /** * Add elements to the head * @param e */public void AddHead (e e) {this.elementdata[0] = e;    }/** * Extension element */private void extendelement (int size) {//capacity if (This.elementData.length = = 0) {    Elementdata = new Object[default_capacity];    }else if (This.elementData.length < size) {empty_elementdata = Elementdata;    Gets the current capacity int oldcapacity = Elementdata.length;    Expansion capacity i + I >> 1 is 1.5 times times the int newcapacity = oldcapacity + (oldcapacity >> 1);    Elementdata = new Object[newcapacity]; 1. Source Array 2. Source Array to copyThe starting position of the 3. Destination Array 4. The starting position of the destination array placement 5. The length of the copy/** * Call the static method of System Arraycopy () to make an array copy * identified as native means the JDK's local library * If frequent expansion will degrade    ArrayList use Performance * Assignment process */system.arraycopy (empty_elementdata,0,elementdata,0,size-1);    }}/** * Deletes an element */public E remove (int index) {rangecheck (index);    E OldValue = elementdata (index);    int nummoved = size-index-1; if (nummoved > 0)//index + 1 is the current index the next assigned to index is all replaced by System.arraycopy (Elementdata, index+1, Elem    Entdata, index, nummoved); Elementdata[--size] = null;    Clearly let the GC do its work//determine if the capacity is current 1/4 is on the shrink capacity do not waste unnecessary memory space shrinkagecapacity ();    return oldValue;    }/** * Delete last * @return */public E Removelast () {return remove (this.size-1); }/** * Determines if the size is greater than * @param index */private void Rangecheck (int index) {if (Index >= siz    e) throw new Indexoutofboundsexception (Outofboundsmsg (index)); }//Output INdex and size private String outofboundsmsg (int index) {return "index:" +index+ ", Size:" +size;    }/** * Gets the element * @param index * @return */public E get (int index) {rangecheck (index);    Return Elementdata (index); /** * Query the value of the current element * @param index * @return */@SuppressWarnings ("unchecked") E elementdata (int ind    Ex) {//Get the element of index position return (E) Elementdata[index]; }/** * Shrink capacity * @param args */public void shrinkagecapacity () {if (size = = ELEMENTDATA.LENGTH/4 &    amp;& ELEMENTDATA.LENGTH/2! = 0) {empty_elementdata = Elementdata;    One-second int oldcapacity = ELEMENTDATA.LENGTH/2;    Elementdata = new Object[oldcapacity];    System.arraycopy (empty_elementdata,0,elementdata,0,size-1); }}//test public static void main (string[] args) {Long then = System.currenttimemillis (); arraylist<integer> ArrayList = new arraylist<> (); Random random = new random (); for (int i = 0; I < 100000; i++) {Arraylist.add (Random.nextint ());} for (int i = 0; i < 99999; i++) {arraylist.remove (0);} Long now = System.currenttimemillis (); System.out.println ("Elapsed Time:" + (Now-then) + "MS"); }}

This is the time to run the above code each person's machine does not work as well as the effect is not the same only for reference

Related Article

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.