解读ArrayList中的add和remove方法 港控/mmm° 2022-03-27 03:08 350阅读 0赞 ### **1. public boolean add(E e)** ### /** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */ public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } 解读: * 首先确认在数组元素加一后,容量是否能保证。 * 之后向数组中最后一个元素的下一个位置放置新的元素。 ### **2.public void add(int index, E element)** ### /** * Inserts the specified element at the specified position in this * list. Shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; } 解读: * 首先判断使用者所想插入的位置index是否合法 private void rangeCheckForAdd(int index) { if (index > size || index < 0) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } * 但凡未落在数组容量之内的位置都将抛出,索引越界异常。 * 之后再判断,插入元素后数组容量是否能保证。 * 然后把索引位置及其索引之后的元素都往后移动。 public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); arraycopy一个本地方法,意味着它底层并非java代码实现。 参数意义为 <table> <tbody> <tr> <td>src</td> <td>srcPos</td> <td>dest</td> <td>destPos</td> <td>length</td> </tr> <tr> <td>把从原数组src</td> <td>的srcPos位置开始的length长度的所有元素</td> <td>复制到dest数组</td> <td>的destPos位置</td> <td>想被复制的个数</td> </tr> </tbody> </table> * 想要插入的位置的元素及其后面的元素都往后移后。 * 往索引位置插入element元素。 ### **3.public E remove(int index)//删除数组中的index位置的对象** ### 根据位置删 /** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to be removed * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { rangeCheck(index); modCount++; E oldValue = 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; } * 检查所要移除的位置是否落在数组元素长度之内。 * 修改次数+1 * 把需要删除的元素赋给oldValue,一遍方法返回旧元素 * numMoved计算出的是删除元素后需要移动的元素数。 * 当numMoved>0 说明删除元素后需要把后面的元素往前移动。 * 当numMoved<0 时,说明删除的元素为最后一个元素。 * 直接把最后一个位置置空。 * 返回被删值 ### **4. public boolean remove(Object o) //删除数组中的o对象** ### 根据内容删 /** * Removes the first occurrence of the specified element from this list, * if it is present. If the list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> * (if such an element exists). Returns <tt>true</tt> if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return <tt>true</tt> if this list contained the specified element */ public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; } * 因为对象是否相等需要使用equals方法,但是空对象不能使用equals方法比较。 * 因此需要先判断传入的对象是否为空。 * 为空时 使用==来寻找数组中空对象并删除。 * 不为空时,便使用equals遍历寻找来删除相应对象。
还没有评论,来说两句吧...