简体   繁体   English

如何在 java 中调整二维数组的大小

[英]How to resize a 2d array in java

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.我有连续采样音频并将其转换为频谱图的代码,但由于 java 线程不准确,频谱图没有恒定的尺寸。

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:例如,使用 BufferedImage 对象和 Image 对象:

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有没有一种我可以实现的算法来调整值数组的大小,就像调整图像大小一样

Arrays in java cannot be resized at all. java 中的 Arrays 根本无法调整大小。 A 'resize' operation makes an entirely new array, copies the data over, and returns the newly created array. 'resize' 操作创建一个全新的数组,复制数据,并返回新创建的数组。

Abstraction is the name of the game: For example, this:抽象是游戏的名称:例如,这个:

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

gives you a list with 500 things in it.给你一个清单,里面有 500 件东西。 If you then add 500 more, that will incur zero penalties.如果您再增加 500 个,则将产生零罚分。

There are a lot of collections already available in the java.util package, as well as java.util.concurrent . 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).有大量用于各种用途的类型,包括原始 collections、collections,并保证性能影响(arraylist 成本低,但前提是您考虑摊销成本:有时对 ZEB3A834246F50C9BDE2C825D74 的添加调用很昂贵,因为它必须制作新的 arrays。如果这是一个问题,有一个库可以解决这个问题 - RealTime java 需要这个很多,这在寻找合适的实现时可能会有所帮助)。

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.然后运行分析器,并配备分析器报告,只查看分析器告诉您的 CPU 和/或 memory 资源正在使用的区域。 There is no point optimizing parts of the code that are <1% of the total resource spend.没有必要优化小于总资源支出 1% 的代码部分。

NB: And, for an encore.. "Resize an array in java" isn't a thing, and... 'A 2D array in java' also doesn't exist.注意:而且,对于一个再来一次......“在 Java 中调整数组大小”不是一回事,而且......“Java 中的二维数组”也不存在。 You can have an array of arrays which is not quite the same thing and presumably what you have here.你可以有一个 arrays 数组,这不是完全相同的东西,大概是你这里的东西。 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.但这并没有改变重点:您需要一个可以做您想做的事情的抽象,而“数组”(或数组的数组)不是它。

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.在 java 中为数组分配大小后,您将无法更改它。如果您需要动态列表大小,请使用 Collections 框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM