Java 中的数组或列表.哪个更快? [英] Array or List in Java. Which is faster?

查看:19
本文介绍了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.

这篇关于Java 中的数组或列表.哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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