何时使用 List而不是长[]?

发布于 2024-10-20 17:39:55 字数 325 浏览 4 评论 0原文

有件事我真的不明白:很多人(参见我的评论)抱怨 Java 并不是真正的面向对象,因为你仍然可以访问原始数据和原始数组。有些人甚至说这些应该消失......

但是我不明白......你能有效地做一些事情,比如信号处理(比如写一个 FFT,对于初学者来说),编写高效的加密算法,快速编写如果您无法访问 Java 中的图像处理库等,例如 int[]long[]

我应该开始使用 List编写 Java 软件吗?而不是长[]?

如果答案是“简单地使用更高级别的库来做你需要的事情”(例如,信号处理),那么这些库应该如何编写?

There's something I really don't understand: a lot (see my comment) of people complain that Java isn't really OO because you still have access to primitive and primitive arrays. Some people go as far as saying that these should go away...

However I don't get it... Could you do efficiently things like signal processing (say write an FFT, for starters), writing efficient encryption algorithms, writing fast image manipulation libraries, etc. in Java if you hadn't access to, say, int[] and long[]?

Should I start writing my Java software by using List<Long> instead of long[]?

If the answer is "simply use higher-level libraries doing what you need" (for example, say, signal processing), then how are these libraries supposed to be written?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡的优雅 2024-10-27 17:39:55

我个人大部分时候都使用List,因为它给你带来了很多方便。您还可以拥有并发集合,但不能拥有并发原始数组。

我使用原始数组的唯一情况几乎是当我读取大量二进制数据时,例如图像处理。我担心实例化egByte对象100M次,尽管我不得不承认我从未尝试过使用那个巨大的Byte列表。我注意到当你有一个 100KB 的文件时,List 工作正常。

大多数图像处理示例等也使用数组,因此在这个领域实际上使用原始数组更方便。

所以总而言之,我对此的实际答案是

使用包装器,除非您正在

  1. 使用非常大的数组
    喜欢长度> 10M(我懒得
    写一个基准!),
  2. 在一个领域工作
    有很多例子或人们喜欢的地方
    原始数组(例如网络
    编程,图像处理),
  3. 你发现有一个重要的
    通过更改为原始数组来提高性能
    实验。
  4. 如果为了任何事
    原因是使用 raw 更容易
    数组为你解决这个问题。

I personally use List most of the times, because it gives you a lot of convenience. You can also have concurrent collections, but not concurrent raw arrays.

Almost the only situation I use raw arrays is when I'm reading a large chunk of binary data, like image processing. I'm concerned instantiating e.g.Byte objects 100M times, though I have to confess I never tried working with that huge Byte list. I noticed when you have something like a 100KB file, List<Byte> works ok.

Most of the image processing examples etc. use array as well, so in this field it's actually more convenient to use raw arrays.

So in conclusion, my practical answer to this is

Use wrappers unless you are

  1. Working with a very large array
    like length > 10M (I'm too lazy to
    write a benchmark!),
  2. Working in a field
    where many examples or people prefer
    raw arrays (e.g. network
    programming, image processing),
  3. You found out there is a significant
    performance gain by changing to raw arrays, by doing
    experiments.
  4. If for whatever
    reason it's easier to work with raw
    arrays on that problem for you.
悍妇囚夫 2024-10-27 17:39:55

在高性能计算中,对象数组(以及基元)至关重要,因为它们可以更稳健地映射到底层 CPU 架构,并且对于缓存访问和垃圾收集等操作的行为更具可预测性。通过这些技术,Java 在人们普遍认为该语言不适合的领域得到了非常成功的应用。

但是,如果您的目标仅仅是编写高度可维护且可证明自我一致的代码,那么更高级别的构造是显而易见的选择。在直接比较中,List 对象隐藏了内存分配、增长列表等问题,并提供(在各种实现中)附加设施,例如堆栈或队列等特定访问模式。使用泛型还可以让您更加自信地进行重构,并获得 IDE 和工具链的全面支持。

开明的开发人员应该针对他们正在接近的用例做出适当的选择。暗示一种语言不够“面向对象”,因为它允许这样的选择,这会让我怀疑这个人要么不相信其他开发人员和他们一样聪明,要么没有对不同应用程序领域有特别广泛的经验。

In high performance computing, arrays of objects (as well as primitives) are essential as they map more robustly onto the underlying CPU architecture and behave more predictably for things such as cache access and garbage collection. With such techniques, Java is being used very successfully in areas where the received wisdom is that the language is not suitable.

However, if your goal is solely to write code that is highly maintainable and provably self consistent, then the higher level constructs are the obvious way to go. In your direct comparison, the List object hides the issue of memory allocation, growing your list and so on, as well as providing (in various implementations) additional facilities such as particular access patterns like stacks or queues. The use of generics also allows you to carry out refactoring with a far greater level of confidence and the full support of your IDE and toolchain.

An enlightened developer should make the appropriate choice for the use case they are approaching. Suggesting that a language is not "OO enough" because it allows such choices would lead me to suspect that the person either doesn't trust that other developers are as smart as they are or has not had a particularly wide experience of different application domains.

向地狱狂奔 2024-10-27 17:39:55

这真的是一个判断力。列表往往可以更好地与通用库配合使用,并且具有诸如 addcontains 等内容,而数组通常速度更快,并且具有内置语言支持,可以用作可变参数。选择您发现的更能满足您的目的的内容。

It's a judgment call, really. Lists tend to play better with generic libraries and have stuff like add, contains, etc, while arrays generally are faster and have built-in language support and can be used as varargs. Select whatever you find serves your purpose better.

野生奥特曼 2024-10-27 17:39:55

好的。

您需要在创建数组时知道数组的大小,但创建后无法更改其大小。但是,列表在创建后可以动态增长,并且它具有 .Add() 函数来执行此操作。

您浏览过此链接吗?

数组与列表的一个很好的比较。

Okay.

You need to know the size of an array at the time that it is created, but you cannot change its size after it has been created. But, a list can grow dynamically after it has been created, and it has the .Add() function to do that.

Have you gone through this link ?

A nice comparison of Arrays vs List.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文