Java 中的数组或列表.哪个更快?
我必须在内存中保留数千个字符串,以便在 Java 中连续访问.我应该将它们存储在数组中还是应该使用某种 List ?
I have to keep thousands of strings in memory to be accessed serially in Java. Should I store them in an array or should I use some kind of List ?
由于数组将所有数据保存在连续的内存块中(与列表不同),使用数组存储数千个字符串会导致问题吗?
Since arrays keep all the data in a contiguous chunk of memory (unlike Lists), would the use of an array to store thousands of strings cause problems ?
我建议你使用分析器来测试哪个更快.
I suggest that you use a profiler to test which is faster.
我个人的意见是你应该使用列表.
My personal opinion is that you should use Lists.
我在大型代码库上工作,之前的一组开发人员无处不在使用数组.它使代码非常不灵活.将其中的大部分内容更改为 Lists 后,我们发现速度没有差异.
I work on a large codebase and a previous group of developers used arrays everywhere. It made the code very inflexible. After changing large chunks of it to Lists we noticed no difference in speed.