面试基础篇——ArrayList扩容机制

简介: 面试基础篇——ArrayList扩容机制

文章目录


引言

扩容规则

1. ArrayList() 无参构造扩容

2. ArrayList(int initialCapacity)扩容

3. public ArrayList(Collection<? extends E> c) 扩容

4. add(Object o)扩容

5. addAll(Collection c) 扩容

其他


引言


要注意的是,以下所有代码中用反射方式来更直观地反映 ArrayList 的扩容特征,但从 JDK 9 由于模块化的影响,对反射做了较多限制,需要在运行测试代码时添加 VM 参数 --add-opensjava.base/java.util=ALL-UNNAMED 方能运行通过


扩容规则


1. ArrayList() 无参构造扩容


ArrayList() 无参构造会使用长度为零的数组

ArrayList()源码

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


2. ArrayList(int initialCapacity)扩容


ArrayList(int initialCapacity) 使用指定容量的数组初始化ArrayList()


ArrayList(int initialCapacity)源码

    /**
     * Constructs an empty list with the specified initial capacity.
     *
     * @param  initialCapacity  the initial capacity of the list
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }


3. public ArrayList(Collection<? extends E> c) 扩容


public ArrayList(Collection<? extends E> c) 会使用 c 集合 的大小作为数组容量


ArrayList(Collection<? extends E> c) 源码

    /**
     * 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) {
        Object[] a = c.toArray();
        if ((size = a.length) != 0) {
            if (c.getClass() == ArrayList.class) {
                elementData = a;
            } else {
                elementData = Arrays.copyOf(a, size, Object[].class);
            }
        } else {
            // replace with empty array.
            elementData = EMPTY_ELEMENTDATA;
        }
    }


4. add(Object o)扩容


add(Object o) 首次扩容为 10,再次扩容为上次容量的 1.5 倍(使用移位相加的规则即( 原 容 量 > > 1 ) + 原 容 量 (原容量>>1)+ 原容量(原容量>>1)+原容量 )


无参构造ArryList()


首次添加元素a

1.png

当数组容量满时添加b

1.png

代码演示:


演示扩容10次的ArrayList的长度

import java.util.ArrayList;
import java.util.List;
public class TestArrayList {
    public static void main(String[] args) {
        System.out.println(arrayListGrowRule(10));
    }
    private static List<Integer> arrayListGrowRule(int n) {
        List<Integer> list = new ArrayList<>();
        int init = 0;
        list.add(init);
        if (n >= 1) {
            init = 10;
            list.add(init);
        }
        for (int i = 1; i < n; i++) {
            init += (init) >> 1;
            list.add(init);
        }
        return list;
    }
}
// 执行结果:[0, 10, 15, 22, 33, 49, 73, 109, 163, 244, 366]


5. addAll(Collection c) 扩容


addAll(Collection c) 没有元素时,扩容为 Math.max(10, 实际元素个数),有元素时为 Math.max(原容量 1.5 倍, 实际元素个数)


代码演示:


无初始容量时扩容

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
// --add-opens java.base/java.util=ALL-UNNAMED
public class TestArrayList {
    public static void main(String[] args) {
        testAddAllGrowEmpty();
    }
    private static void testAddAllGrowEmpty() {
        ArrayList<Integer> list = new ArrayList<>();
        list.addAll(List.of(1, 2, 3));    
//        list.addAll(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
        System.out.println(length(list));
    }
    public static int length(ArrayList<Integer> list) {
        try {
            Field field = ArrayList.class.getDeclaredField("elementData");
            field.setAccessible(true);
            return ((Object[]) field.get(list)).length;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
//当执行 list.addAll(List.of(1, 2, 3)); 时运行结果为:10
//当执行 list.addAll(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); 时结果为:11

有初始容量(10) 时扩容

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
// --add-opens java.base/java.util=ALL-UNNAMED
public class TestArrayList {
    public static void main(String[] args) {
        testAddAllGrowNotEmpty();
    }
    private static void testAddAllGrowNotEmpty() {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        list.addAll(List.of(1, 2, 3));
//        list.addAll(List.of(1, 2, 3, 4, 5, 6));
        System.out.println(length(list));
    }
    public static int length(ArrayList<Integer> list) {
        try {
            Field field = ArrayList.class.getDeclaredField("elementData");
            field.setAccessible(true);
            return ((Object[]) field.get(list)).length;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
//当执行 list.addAll(List.of(1, 2, 3)); 时运行结果为:15
//当执行 list.addAll(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); 时结果为:16


其他


面试基础篇——ArrayList VS LinkedList

面试基础篇——快速排序

面试基础篇——二分查找

相关文章
|
7月前
|
Java 开发者 容器
【面试题精讲】JVM-打破双亲委派机制-OSGI模块化
【面试题精讲】JVM-打破双亲委派机制-OSGI模块化
|
7月前
|
存储 Java 索引
【面试题精讲】ArrayList 和 Array(数组)的区别?
【面试题精讲】ArrayList 和 Array(数组)的区别?
|
1天前
|
消息中间件 安全 前端开发
字节面试:说说Java中的锁机制?
Java 中的锁(Locking)机制主要是为了解决多线程环境下,对共享资源并发访问时的同步和互斥控制,以确保共享资源的安全访问。 锁的作用主要体现在以下几个方面: 1. **互斥访问**:确保在任何时刻,只有一个线程能够访问特定的资源或执行特定的代码段。这防止了多个线程同时修改同一资源导致的数据不一致问题。 2. **内存可见性**:通过锁的获取和释放,可以确保在锁保护的代码块中对共享变量的修改对其他线程可见。这是因为 Java 内存模型(JMM)规定,对锁的释放会把修改过的共享变量从线程的工作内存刷新到主内存中,而获取锁时会从主内存中读取最新的共享变量值。 3. **保证原子性**:锁
16 1
|
1天前
|
Java
[Java 面试题] ArrayList篇
[Java 面试题] ArrayList篇
|
1天前
|
安全 前端开发 Java
大厂面试题详解:聊一下双亲委派机制
字节跳动大厂面试题详解:聊一下双亲委派机制
29 0
|
1天前
|
运维 监控 Java
微服务心跳监测机制讲解与实现,与面试过程中如何回答这个问题
微服务心跳监测机制讲解与实现,与面试过程中如何回答这个问题
34 0
|
1天前
|
安全 编译器 C++
【Qt 面试题】Qt信号槽机制与优势与不足
【Qt 面试题】Qt信号槽机制与优势与不足
37 1
|
1天前
|
存储 缓存 Java
什么!?实战项目竟然撞到阿里面试的原题!???关于MyBatis Plus的缓存机制
什么!?实战项目竟然撞到阿里面试的原题!???关于MyBatis Plus的缓存机制
|
1天前
面试题之:ArrayList和LinkedList有哪些区别
面试题之:ArrayList和LinkedList有哪些区别
|
1天前
|
监控 Java 程序员
GC(垃圾处理机制)面试加薪必备
GC(垃圾处理机制)面试加薪必备
25 0