gpt4 book ai didi

java - Arraylist 相当于 arraycopy

转载 作者:行者123 更新时间:2023-12-01 18:16:13 28 4
gpt4 key购买 nike

我正在学习android,并试图制作一个涉及傅立叶变换的应用程序。我找到了一些示例代码,但它是针对 double 的,我想使用 float 和数组列表来代替。我更改了大部分代码,但不知道如何使用 arraycopy 更改代码行。代码如下:

public static void fourierTrans(ArrayList<Float> array) {

FloatFFT_1D fftDo = new FloatFFT_1D(array.size());
float[] fft = new float[array.size() * 2];
System.arraycopy(array, 0, fft, 0, array.size()); //This line here
fftDo.realForwardFull(fft);

/*for(float d: fft) {
System.out.println(d);
}*/
}

有谁知道带有 float 的数组列表的 arraycopy 等效项吗?

最佳答案

在这个简单的例子中,您可以使用以下习惯用法:

ArrayList<Float> myCopy = new ArrayList<Float>(array);

它将创建一个新的 ArrayList<Float>包含 array 中的所有值争论。

现在有一个更复杂的习惯用法,您想要限制复制内容的索引和大小:

ArrayList<Float> foo = new ArrayList<Float>();
foo.addAll(Arrays.asList(1f,2f,3f,4f,5f));
ArrayList<Float> bar = new ArrayList<Float>();
// here it copies the values from the other list,
// but limits the size to the other list's size / 2 (rounded down)
bar.addAll(foo.subList(0, foo.size() / 2));
System.out.println(bar);

输出

[1.0, 2.0]

关于java - Arraylist 相当于 arraycopy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29351340/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com