如何在Java中调整2D数组的大小

huangapple go评论82阅读模式
英文:

How to resize a 2d array in java

问题

为了背景信息,我正在开发一个能够分类语音并最终控制视频游戏角色的人工智能。我已经编写了连续采样音频并将其转换为频谱图的代码,但由于Java线程的不准确性,频谱图的尺寸不是恒定的。

我有一个表示频谱图的二维值数组,我想调整这个数组的大小。我目前找到的方法效率不够高,因为时间非常紧迫。例如,使用BufferedImage对象和Image对象:

Image newImage = yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);

我是否可以实现一个算法来调整值数组的大小,就像调整图像大小一样。

英文:

For context, I am working on an AI that classifies speech and ultimately controls a character in a video game. I have code that continuously samples audio and converts it into a spectrogram, but due to java threads being inaccurate the spectrograms don't have constant dimensions.

I have a 2D array of values that represent the spectrogram and I want to resize the array. The only current methods I've found aren't efficient enough as time is of the essence. For example, using BufferedImage objects and Image objects:

Image newImage = yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);

Is there an algorithm I can implement that resizes the array of values, the same way an image is resized

答案1

得分: 1

在Java中,数组是无法被重新调整大小的。一个“调整大小”操作会创建一个全新的数组,将数据复制过去,并返回新创建的数组。

抽象化是关键:例如,下面这段代码:

List<String> list = new ArrayList<String>(1000);
for (int i = 0; i < 500; i++) list.add("Hello, " + i);

会给你一个包含500个元素的列表。如果随后再添加500个元素,不会产生任何额外开销。

java.util包以及java.util.concurrent中已经提供了许多集合。对于更特殊的需求,可以看看第三方库;有大量不同类型的集合可供选择,包括原始类型的集合、对性能影响有保证的集合(ArrayList的开销很低,但仅当你将成本摊销考虑时如此:偶尔向ArrayList添加元素会变得昂贵,因为它必须创建新数组。如果这是个问题,有一些库可以解决这个问题 - 实时Java在这方面需要很多帮助,在寻找适当的实现时可能会有所帮助)。

需要注意的是,在没有实际证据表明代码运行速度过慢之前,不必担心性能问题。当你拥有一个性能分析器报告后,只有那些性能分析器指出CPU和/或内存资源消耗较大的区域才值得关注。优化总资源消耗小于总资源消耗的1%的代码是没有意义的。

另外,需要注意的是,“调整大小一个数组在Java中”并不是一个概念,以及,“Java中的二维数组”也不存在。你可以有一个数组的数组,虽然不完全相同,但应该是你在这里所描述的情况。但是这并不改变关键点:你需要一个能够实现你要求的抽象,而“数组”(或数组的数组)并不是这个抽象。

英文:

Arrays in java cannot be resized at all. A 'resize' operation makes an entirely new array, copies the data over, and returns the newly created array.

Abstraction is the name of the game: For example, this:

List&lt;String&gt; list = new ArrayList&lt;String&gt;(1000);
for (int i = 0; i &lt; 500; i++) list.add(&quot;Hello, &quot; + i);

gives you a list with 500 things in it. If you then add 500 more, that will incur zero penalties.

There are a lot of collections already available in the java.util package, as well as java.util.concurrent. For more exotic needs, have a look at third party offerings; there are tons and tons of types out there for varied uses, including primitive collections, collections with guarantees about performance impact (arraylist has low cost but only if you consider costs amortized: From time to time an add call to an arraylist is expensive as it has to make new arrays. If that's a problem, there's a library for that - RealTime java needs this a lot, that might help when looking for appropriate implementations).

Note that you shouldn't worry about performance until you have a real life 'proof' that the code is running slower than you'd like. Then run profilers, and armed with a profiler report, look at only those areas that the profiler tells you are where the CPU and/or memory resources are being spent. There is no point optimizing parts of the code that are <1% of the total resource spend.

NB: And, for an encore.. "Resize an array in java" isn't a thing, and... 'A 2D array in java' also doesn't exist. You can have an array of arrays which is not quite the same thing and presumably what you have here. But that doesn't change the point: You need an abstraction that does what you want, and 'array' (or array of arrays) is not it.

答案2

得分: 0

一旦你在Java中为数组分配了大小,就无法更改它。如果你需要具有动态列表大小,请使用集合框架。

英文:

Once you assigned a sized to an array in java you can't change it.Use Collections framework if you need to have a dynamic list size.

huangapple
  • 本文由 发表于 2020年7月26日 03:36:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63092732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定