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