爪哇 - 列表或数组? [英] Java - List or Array?

查看:168
本文介绍了爪哇 - 列表或数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道列表让事情在Java中容易得多,而不是通过硬阵列集工作(列表允许您添加/删除随意的元素,他们自动的调整,等等)。

I know Lists make things much easier in Java instead of working with hard-set arrays (lists allow you to add/remove elements at will and they automagically resize, etc).

我读过一些东西,这表明阵列应该在java中尽可能避免,因为它们不是在所有灵活(有时可以并处奇怪的限制,例如,如果你不知道数组必须大小,等等)。

I've read some stuff suggesting that array's should be avoided in java whenever possible since they are not flexible at all (and sometimes can impose weird limitations, such as if you don't know the size an array needs to be, etc).

这是良好做法停止使用数组所有,只使用清单的逻辑呢?我敢肯定列表类型消耗不是一个数组的内存,因此具有更高的开销,但是这是显著?大多数列表会在运行时,如果他们留反正周围铺设所以也许它并不像什么大不了的事,因为我想将GC'ed?

Is this "good practice" to stop using arrays at all and use only List logic instead? I'm sure the List type consumes more memory than an array and thus have higher overhead, but is this significant? Most Lists would be GC'ed during runtime if they are left laying around anyways so maybe it isn't as big of a deal as I'm thinking?

推荐答案

我觉得一个很好的经验法则是使用列表除非你需要一个阵列(内存/性能原因)。否则列表通常更容易维护,因此不太可能导致未来的错误。

I think a good rule of thumb would be to use Lists unless you NEED an Array (for memory/performance reasons). Otherwise Lists are typically easier to maintain and thus less likely to cause future bugs.

在列表自动扩展性方面提供了更多的灵活性/功能,所以,除非你是pssed内存无论是$ P $(不起列出创建开销)或不介意保持数组的大小,因为它扩大/缩小,我会推荐列表。

Lists provide more flexibility/functionality in terms of auto-expansion, so unless you are either pressed for memory (and can't afford the overhead that Lists create) or do not mind maintaining the Array size as it expands/shrinks, I would recommend Lists.

尽量不要插手code太多,转而关注更为明显和可读性的组件。

Try not to micromanage the code too much, and instead focus on more discernible and readable components.

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

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