标签:dal cli 选择 static 需要 exception write code ESS
ArrayList也叫作数组列表
public static void main(String[] args) { List list1 = new ArrayList<String>(); list1.add("a"); //0100=4 右移1位 0010=2 System.out.println(4>>1); //相当于 4/2 //0010=2 左移1位 0100=4 System.out.println(2<<1); //相当于 2*2 } transient Object[] elementData; private int size; private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; /** * Default initial capacity. */ private static final int DEFAULT_CAPACITY = 10; protected transient int modCount = 0; private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; public boolean add(E e) { // 保证数组的容量始终够用 ensureCapacityInternal(size + 1); // Increments modCount!! // size是elementData数组中元素的个数,初始化为0 elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { // 如果数组没有元素,给数组一个默认大小,会选择实例化时的值与默认大小中较大值 if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } // 保证容量够用 ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity(int minCapacity) { // modCount是elementData数组发生size更改的次数 modCount++; // 如果当前数组长度比原数组长度大,则进行扩容 if (minCapacity - elementData.length > 0) grow(minCapacity); } private void grow(int minCapacity) { int oldCapacity = elementData.length; // 新的数组长度 会进行行1.5倍扩容,通过向右移位实现/2操作 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:这里只用Arrays.copyOf elementData = Arrays.copyOf(elementData, newCapacity); } public void add(int index, E element) { // 判断index的值是否合法,如果不合法则抛出异常 rangeCheckForAdd(index); // 保证容量够用 ensureCapacityInternal(size + 1); // Increments modCount!! // 从第index位置开始,将元素往后移一个位置 System.arraycopy(elementData, index, elementData, index + 1, size - index); // 把要插入的元素e放在第index的位置 elementData[index] = element; // 数组的元素个数加1 size++; } /** * A version of rangeCheck used by add and addAll. */ private void rangeCheckForAdd(int index) { if (index > size || index < 0) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } /** * Constructs an IndexOutOfBoundsException detail message. * Of the many possible refactorings of the error handling code, * this "outlining" performs best with both server and client VMs. */ private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size; }
ArrayList是基于数组实现的,是一个动态数组,其容量能够自动增长 ArrayList不是线程安全的,只能用在单线程环境下,多线程环境需要使用Collections同步方法。Collections.synchronizedList(List l)返回一个线程安全的ArrayList。如果是读写比例比较大的话(锁粒度降级),则可以考虑CopyOnwriteArrayList。
标签:dal cli 选择 static 需要 exception write code ESS
原文地址:https://www.cnblogs.com/atomicbomb/p/9917076.html