1、面向对象补充(详见面试补充) 基于JDK11
静态代码块 > 非静态代码块 > 无参/有参构造
在同一次编译运行时,静态代码块只会被调用一次
List
ArrayList (数组,初始容量为10)
注:
除了通过 Iterator 自己的remove或add方法,迭代器将抛出ConcurrentModificationException 。 因此,面对并发修改,迭代器快速而干净地失败,而不是在未来不确定的时间冒着任意、非确定性行为的风险。可被序列化,线程不安全
安全的List集合:
CopyOnWriteArrayList:官方解释意思大概为(由百度翻译)
ArrayList线程安全变体,其中所有可变操作( add 、 set等)都是通过制作底层数组的新副本来实现的。
这通常成本太高,但当遍历操作大大超过突变时可能比替代方法更有效,并且当您不能或不想同步遍历时很有用,但需要排除并发线程之间的干扰。 “快照”样式的迭代器方法使用对创建迭代器时数组状态的引用。 这个数组在迭代器的生命周期内永远不会改变,所以干扰是不可能的,并且迭代器保证不会抛出ConcurrentModificationException 。 自创建迭代器以来,迭代器不会反映对列表的添加、删除或更改。 不支持迭代器本身的元素更改操作( remove 、 set和add )。 这些方法抛出UnsupportedOperationException 。
允许所有元素,包括null 。
内存一致性影响:与其他并发集合一样,在将对象放入CopyOnWriteArrayList之前线程中的操作发生在另一个线程中从CopyOnWriteArrayList访问或删除该元素之后的操作之前。Collections.synchronizedList(list);
ArrayList容量增加策略,>>1,右移一位除以2,即新容量在OldCapacity的基础上加OldCapacity的一半
/**
     * Returns a capacity at least as large as the given minimum capacity.
     * Returns the current capacity increased by 50% if that suffices.
     * Will not return a capacity greater than MAX_ARRAY_SIZE unless
     * the given minimum capacity is greater than MAX_ARRAY_SIZE.
     *
     * @param minCapacity the desired minimum capacity
     * @throws OutOfMemoryError if minCapacity is less than zero
     */
    private int newCapacity(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity <= 0) {
            if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
                return Math.max(DEFAULT_CAPACITY, minCapacity);
            if (minCapacity < 0) // overflow
                throw new OutOfMemoryError();
            return minCapacity;
        }
        return (newCapacity - MAX_ARRAY_SIZE <= 0)
            ? newCapacity
            : hugeCapacity(minCapacity);
    }最大容量默认为Integer的最大值为2的31次方-1 -8
/**
     * The maximum size of array to allocate (unless necessary).
     * Some VMs reserve some header words in an array.
     * Attempts to allocate larger arrays may result in
     * OutOfMemoryError: Requested array size exceeds VM limit
     */
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;如果容量超过了这个值,则会使用Integer的最大值
private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE)
            ? Integer.MAX_VALUE
            : MAX_ARRAY_SIZE;
    }List在对容量进行动态增加时,使用的是数组copy
Arrays.copyOf(elementData, size)
System.arraycopy(elementData, 0, a, 0, size);补:数组Copy
Arrays.copyOf:
System.arraycopy:
LinkedList(链表 -> 双向链表)
注:
除了通过 Iterator 自己的remove或add方法,迭代器将抛出ConcurrentModificationException 。 因此,面对并发修改,迭代器快速而干净地失败,而不是在未来不确定的时间冒着任意、非确定性行为的风险。Arrays
注意点:arrays.asList()
/**
 * 不能对Arrays.asList转换成的List进行remove或者add操作,否则会报一个异常
 * Exception in thread "main" java.lang.UnsupportedOperationException
 * 原因好像是因为Object的转换
 * 调用流程
 * @SafeVarargs
 *  @SuppressWarnings("varargs")
 *  public static <T> List<T> asList(T... a) {
 *      return new ArrayList<>(a);
 * }
 * ArrayList(E[] array) {
 *         a = Objects.requireNonNull(array);
 * }
 * public static <T> T requireNonNull(T obj) {
 *  if (obj == null)
 *      throw new NullPointerException();
 *  return obj;
 * }
 */
 
  
  
  
  
 
