一天一个类--ArrayList之二

it2022-05-29  71

继续我的小激动~~~

1、看看构造一个ArrayList 有两种方式

一个指定大小,一个不指定。我们知道他其实使用数组来实现了,数组肯定要有大小,那么他没指定大小,默认的是多少呢???追踪源码---开启万里追踪模式~~~

 

/** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { this(10); }

知道了,就是10~~。

2、还有一个通过现有的集合类来实现构造。怎么实现的啊???

/** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); size = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } View Code

 

原来是调用Arrays.copyOf() 这个工具类。

3、插入元素add()

  看起来好简单的方法,插入就行了呗。

诸位看官,有没有考虑,插不进去咋办?满了!~

/** * Increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * * @param minCapacity the desired minimum capacity */ private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } View Code

int newCapacity = oldCapacity + (oldCapacity >> 1); 

动态扩容,很显然变为了原来的1.5倍。【在JDK6中 int newCapacity = oldCapacity *3/2+1;】

4、查找 indexOf()

  其实还不是一个一个的遍历!!但是不要忘了咱们的ArrayList可以存null的~

  这就要分类了,看看源码的

/** * Returns the index of the first occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the lowest index <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * or -1 if there is no such index. */ public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } View Code

  如果是null 就遍历所有的,使用==

  如果不是,还要遍历 使用equals

  【从这里我们就知道了,二者是不一样的】哇塞!~

5、clone / toArray()

  怎么克隆?? 还是Arrays.copyOf()!!!

6、在指定位置插入元素 add(int index, E elements)

  咋个实现?挪呗!~index 后面的元素,乖乖的向后走一个,给我个地方~!

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++; } View Code

  现在知道删除remove(index)是如何实现的了吧?但是挪了之后,最后一个元素怎么处理??置null让gc自己处理。不信自己看源码!~

  那么,删除指定元素呢?(在我们ArrayList中指的是删除第一个出现的元素。)如果这个元素是null ? 你可依然记得indexOf()里面怎么实现的(== 和 equals) ,yes!还是分两种情况查找,删除!

7、clear()

  全部元素置null

这个ArrayList 就是这么多了 ~ 么么嗒

转载请注明:http://home.cnblogs.com/u/plxx/

转载于:https://www.cnblogs.com/plxx/p/4439513.html


最新回复(0)