ArrayList and homemade imitation ArrayList

Source: Internet
Author: User

Recently in the internship by the company sent to the Valley training, in the teacher's layout to do, theoretically with ArrayList is very convenient, only he has not said, I use the words have a feeling of cheating, then the whim of his own with an array to write an imitation ArrayList.

/** *  @ClassName: mylist *  @Description:  imitation arraylist*  @author  lmc*   @date  2015-11-5  pm 06:34:59 */public class mylist<t>{/** * @ fields defaultsize :  Initialize array length *  default length should be 10, for easy testing   2*/private int defaultsize  = 2;  private static final int max_array_size = integer.max _value - 8;//t[] entity;/** *  @Fields  nowSize :  Current array length */int size= 0; object[] elementdata;public mylist () {elementdata = new object[defaultsize];} Public mylist (int listsize) {if  (listsize < 0)              throw new illegalargumentexception ("Illegal Capacity:  "+            listsize);elementData =  New object[listsizE];} /** *  @Title: add  Add an element *  @Description:    */public void add ( T element) {//Determines whether the number of elements in the array exceeds the upper limit, expands the array ensurecapacityinternal (size+1),//size+1 to the required minimum capacity elementdata[size++]  = element;} /** *  @Title: get *  @Description:  Gets the first index element */public t get (int  Index) {rangecheck (index);return  (T) elementdata[index];}     /**     *  @Title: remove      *  @Description:   Remove the index element in list     *  @return  T       index Element     */    public t remove ( Int index)  {        rangecheck (index);         T oldValue =   (T) elementdata[index];         int nummoved = size - index - 1;        if  (numMoved  > 0)             system.arraycopy ( elementdata, index+1, elementdata, index,                               nummoved);        elementdata[--size] = null;  // clear to let GC do its work         return oldValue;    }/** *  @Title: size  *  @Description:    Gets the number of current data (or the length of the list) */public int size () {return size;} /** *  @Title: rangecheck *  @Description:  determine if index exceeds array range       */private void raNgecheck (Int index)  {if  (index < 0 | |  index >= this.size) throw new indexoutofboundsexception (OUTOFBOUNDSMSG (index));} /** *  @Title: outofboundsmsg *  @Description:  Array out-of-bounds hint information */private string  Outofboundsmsg (Int index)  {        return  "index:  "+index+", size:  "+this.size;    }/** *  @Title:  ensurecapacityinternal *  @Description:  To determine if the array size needs to be amplified *  @param        size  the actual number of elements in the current array */private void ensurecapacityinternal (int mincapacity)  { if (mincapacity - elementdata.length > 0) {grow (mincapacity);}} /** *  @Title: grow *  @Description:  increase the range     array size of the array to 1.5 times times the original *  (jdk1.7) new features if the expansion is still less than the minimum demand capacity, the direct expansion to the minimum demand capacity    bit operation Speed *  (jdk1.6 before) is expanded to 1.5 times times +1;*/private Void grow (int mincapacity) {int oldcapacity = elementdata.length;     int newCapacity = oldCapacity +  (oldcapacity >> 1);//object[]  newelementdata = new object[newcapacity];//system.arraycopy (elementData, 0,  newelementdata, 0, nowsize);    if  (newcapacity - mincapacity  < 0)             newcapacity =  minCapacity;        if  (Newcapacity - max_array_ size > 0)             newcapacity  = hugecapacity (mincapacity);     elementdata = arrays.copyof ( elementdata, newcapacity);    // system.out.println ("Start expanding array size");}  /** *  @Title: hugecapacity * The   capacity of the @Description:  processing expansion has exceeded the defined maximum capacity   */private static int hugecapacity (int  mincapacity)  {        if  (mincapacity < 0 )  // overflow            throw new  outofmemoryerror ();        return  (minCapacity >  max_array_size)  ?             Integer.max_value :            max_array_size ;     }}



ArrayList and homemade imitation ArrayList

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.