In-depth understanding of the---------of the Java Collection Framework ArrayList collection-----Adding methods

Source: Internet
Author: User
Tags addall

ArrayList Collection-----Add method

1, add (e) Adding elements to the collection

  

/** * Check if the array capacity is sufficient * @param mincapacity */public void ensurecapacity (int mincapacity) {Modcount++;int oldcapacity = Elementda Ta.length;if (Mincapacity > Oldcapacity) {Object olddata[] = elementdata;int newcapacity = (oldcapacity * 3)/2+1;if ( Newcapacity < mincapacity) {newcapacity  = Mincapacity;elementdata = arrays.copyof (Elementdata, newCapacity);}}}
/* (non-javadoc) * add element to ArrayList * @see Java.util.abstractlist#add (Java.lang.Object) */public boolean add (E e) { Ensurecapacity (size+1); elementdata[size++] = E;return true;}

2, add (int index, e e) Adds an element to the collection at the specified index

  

/**     * Inserts the specified element at the specified position in this     * list. Shifts the element currently at, if any, and * any     subsequent elements to the right (adds one to their I ndices).     *     * @param index index at which the specified element was to being inserted     * @param element element to be Inserted
   * @throws indexoutofboundsexception {@inheritDoc}     *    /public void Add (int index, E element) {if (Index > size || Index < 0)    throw new Indexoutofboundsexception ("Index:" +index+ ", Size:" +size); ensurecapacity (size+1);  Increments modcount!! System.arraycopy (Elementdata, index, Elementdata, index + 1, size-index); Elementdata[index] = element;size++;    }

3, AddAll (collection<? Extends e> c) Add collection

 

  /* * add Element Collection * (non-javadoc) * @see Java.util.abstractcollection#addall (java.util.Collection) */public Boolean AddAll (collection<? extends e> c) {object[] a = C.toarray (); int numnew = A.length;ensurecapacity (size+numnew); System.arraycopy (A, 0, elementdata, size, numnew); Size+=numnew;return numnew!=0;}

4, AddAll (int index, COLLECTION<?) extends e> c); Adds a collection at the specified index

  

public boolean addall (int index, COLLECTION<? extends e> c) {if (Index > Size | | Index < 0)    throw new Inde Xoutofboundsexception ("index:" + index + ", size:" + size); object[] A = C.toarray (); int numnew = A.length;ensurecapacity (size + numnew);  increments modcountint nummoved = size-index;if (nummoved > 0)    system.arraycopy (elementdata, index, ElementDat A, index + numnew,     nummoved);        System.arraycopy (A, 0, Elementdata, index, numnew); size + numnew;return numnew! = 0;    }

  

In-depth understanding of the---------of the Java Collection Framework ArrayList collection-----Adding methods

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.