ArrayList扩容
在add时先判断长度
![](https://app.yinxiang.com/shard/s64/res/ad063255-f150-4fd4-99e6-9415827b538f/Z%7D%40C%24FUZ%40AQO74C%7BXHU%7B1JU.png)
minCapacity是原数组长度+1,Math.max是通过三元运算符判断大小并返回数据
![](https://app.yinxiang.com/shard/s64/res/004a970f-16b2-4023-b414-e3b8b1496da2/%28%40BC6P%4007O4RDNIKMX61F3.png)
![](https://app.yinxiang.com/shard/s64/res/004a970f-16b2-4023-b414-e3b8b1496da2/%28%40BC6P%4007O4RDNIKMX61F3.png)
modCount是用来记录当前数组修改的次数,minCapacity减去数组长度大于0说明数组长度不够,需要扩容
![](https://app.yinxiang.com/shard/s64/res/356875c0-ac00-41b4-bfec-9e32fecfe82b/%5D%28%7B63S82%7B3%24S5KM%5DOP53KW3.png)
![](https://app.yinxiang.com/shard/s64/res/356875c0-ac00-41b4-bfec-9e32fecfe82b/%5D%28%7B63S82%7B3%24S5KM%5DOP53KW3.png)
在这里进行扩容-newCapacity,同时判断扩容后的值是否大于minCapacity,如果小于,重新给newCapacity赋值,最后再调用Arrays.copyOf方法
![](https://app.yinxiang.com/shard/s64/res/c436dec7-103b-4d1c-8d94-c06053266288/ZVLWG1QZ1%285YUFJ%7D%5DJMRYGX.png)
![](https://app.yinxiang.com/shard/s64/res/c436dec7-103b-4d1c-8d94-c06053266288/ZVLWG1QZ1%285YUFJ%7D%5DJMRYGX.png)
![](https://app.yinxiang.com/shard/s64/res/965adf56-2af1-44c6-8442-119a3bfa4974/P1MIHM%401S%7BKS0Z%24OD%40SOS2.png)
调用System.arraycopy底层方法实现复制数组并返回从而实现扩容,
![](https://app.yinxiang.com/shard/s64/res/28f81dd1-63e6-4035-9e79-bfcaaa6cb6d6/M7UZ%7BZ%401%28DZW%5B%7ECQE%29M%28UD.png)
addAll方法
![](https://app.yinxiang.com/shard/s64/res/6394b1d0-c8f9-4a92-889b-4d0f843190ef/8%7DB6F%7DVJU%24%282111%40%29NC9NHD.png)
博客链接:https://www.cnblogs.com/gqymy/