为什么preferred使用列表而不是数组在Java中? [英] Why is it preferred to use Lists instead of Arrays in Java?

查看:176
本文介绍了为什么preferred使用列表而不是数组在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议给我们许多个人和作者使用列表比数组。

Many people and authors suggested to us to use list than array.

List <Integer> list = new ArrayList<Integer>();
list.addElement(1);
....

这是什么背后的原因是什么?

What it is the reason behind it?

推荐答案

您应该大致preFER选择适合这个职位的合适的数据结构。你需要了解你手头的任务,以及所有你有不同的选择,以及它们如何在迭代方面比较和搜索,添加,删除和插入数据。一般情况下,你需要理解数据结构是如何访问和操纵内存,并选择最佳的数据结构,根据你如何预测你的申请将被使用。

You should generally prefer to choose the right data structure for the job. You need to understand your task at hand as well as all the different options you have and how they compare in terms of iteration, and searching, and adding, removing, and inserting data. In general, you need to understand how the data structure accesses and manipulates memory and choose the best data structure based on how you anticipate your application will be used.

显然,它并不总是清晰。但是你可以理解为理想不同的数据结构。

Obviously, it isn't always clear-cut. But you can understand the ideals for different data structures.

例如,在你只迭代,而不需要搜索纯静态的,固定长度的数据,是理想的一个数组。这是常见的密码算法使用这些数组。如果数据是静态的,而是代替迭代,你需要搜索,你可能要某种类型的树结构。如果你想快速插入,散列可能是理想的。如果数据经常变化,你想有一个结构,它是有效率在改变它的大小,就像一个列表。

For example, purely static, fixed length data in which you'll only iterate, without a need for search, is ideal for an array. It's common to use such arrays in cipher algorithms. If the data is static but instead of iterating, you need to search, you might want some type of tree structure. If you want fast insertion, hashing is probably the ideal. If the data changes often, you want a structure that is efficient at changing its size, like a list.

当然,有许多变型和设计,以解决各种具体问题的数据结构的组合。之所以有这么多是因为他们编写高效的程序发挥的重要性。不管怎样,我的观点是,了解数据结构。了解每个理想的情况,那么你就能够决定或设计合适的数据结构的任何任务。

Of course, there's many variations and combinations of data structures designed to solve all kinds of specific problems. The reason there are so many is because of the importance they play in writing efficient programs. Anyway, my point is, learn about data structures. Understand the ideal situations for each and then you'll be able to decide or design suitable data structures for any task.

这篇关于为什么preferred使用列表而不是数组在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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