Java 6SE 中 ArrayList 的增长策略 [英] Growth Policy of ArrayList in Java 6SE

查看:13
本文介绍了Java 6SE 中 ArrayList 的增长策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人知道 Java 1.6 中 ArrayList 的增长策略?Java 文档说

I was wondering if anyone knows the growth policy of ArrayList in Java 1.6? The java doc says

除了添加元素具有恒定的摊销时间成本之外,没有指定增长策略的细节.

The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

但我只是想知道细节,因为我知道我要开始的尺寸,但我想确保初始尺寸足够大,不会导致立即调整大小.我知道使用 HashMap 你可以设置一个负载因子在后台是否有类似的事情发生?还是在空间用完时它总是在增长?

But I just wonder the details because I know the size I'm targeting to start but I want to make sure I'm making the initial size big enough to not cause an instant resize. I know with HashMap you can set a load factor is there something similar happening in the back ground? Or is it always growing when space is out?

推荐答案

ArrayList 不需要 loadFactor,因为它总是在 100% 填充时增长,因此您可以使用事先知道的大小创建它,并且如果您稍后填充那么多元素,则不会增长.另一方面,哈希表填充得越多,效率就越低,因此您可以使用 loadFactor 在性能和浪费的空间之间进行权衡,但对于像 ArrayList 这样的可增长数组,情况并非如此.

ArrayList doesn't need a loadFactor as it always grows when it is 100% filled, so you can create it with exactly the size you know in advance and it won't grow if you fill that many elements in later. Hashtables on the other hand become ever more inefficient the more they are filled, and thus you can ajust the tradoff between performance and wasted space with the loadFactor, but this isn't the case for growable Arrays like ArrayList.

这篇关于Java 6SE 中 ArrayList 的增长策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆