BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #55105同步于 2017/2/11
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

【问题】ArrayList扩容机制为啥是1.5倍+1?

workingloong
2017/2/11镜像同步15 回复
Java ArrayList扩容机制是1.5倍+1,最近面试被问为啥选取1.5倍,而不是2倍或者其他的?
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
silenceTYN机器人#1 · 2017/2/11
关注。跟hash同理? 【 在 workingloong 的大作中提到: 】 : Java ArrayList扩容机制是1.5倍+1,最近面试被问为啥选取1.5倍,而不是2倍或者其他的? : 发自「贵邮」
lt0506机器人#2 · 2017/2/11
cpp vector是2倍
Sluggard机器人#3 · 2017/2/11
int newCapacity = oldCapacity + (oldCapacity >> 1); 因为比特操作快!我猜的。我去谷歌一下
saiy机器人#4 · 2017/2/11
保证最大浪费最小 发自「贵邮」
cgp机器人#5 · 2017/2/11
这是以前程序员得出的经验值 【 在 workingloong (workingloong) 的大作中提到: 】 : Java ArrayList扩容机制是1.5倍+1,最近面试被问为啥选取1.5倍,而不是2倍或者其他的? : --
Sluggard机器人#6 · 2017/2/11
The harm is that the bigger the size of the ArrayList, the more memory allocated to it (which could go to waste if the space is not used). Since increasing the capacity of the ArrayList is much less expensive than increasing the capacity of a HashMap, it makes sense to be more conservative with the increase of capacity of an ArrayList. 就是说,ArrList的扩容消费相对HashMap更小,所以允许ArrList更频繁的扩容。
leihangwang机器人#7 · 2017/2/11
赞~ 【 在 Sluggard (Sluggard) 的大作中提到: 】 : The harm is that the bigger the size of the ArrayList, the more memory allocated... : 就是说,ArrList的扩容消费相对HashMap更小,所以允许ArrList更频繁的扩容。
workingloong机器人#8 · 2017/2/11
赞,干货! 【 在 Sluggard (Sluggard) 的大作中提到: 】 : The harm is that the bigger the size of the ArrayList, the more memory allocated to it (which could go to waste if the space is not used). Since increasing the capacity of the ArrayList is much less expensive than increasing the capacity of a HashMap, it makes sense to be more conservative with the increase of capacity of an ArrayList. : 就是说,ArrList的扩容消费相对HashMap更小,所以允许ArrList更频繁的扩容。 通过『我邮2.0』发布
lance6716机器人#9 · 2017/2/11
最佳实践