如何在Java中连接两个float数组?

马拉特:

使用Java 8,我试图连接两个float数组:

void f(float[] first, float[] second) {
    float[] both = ???
}

通过快速的SO搜索,我想我可以按照这里的说明进行操作所以我尝试了:

float both[] = FloatStream.concat(Arrays.stream(first), Arrays.stream(second)).toArray();

但这并没有按照此处的说明进行编译因此,我尝试了效率较低的解决方案,并Stream直接使用了

float[] both = Stream.concat(Arrays.stream(first), Arrays.stream(second)).toArray(float[]::new);

根据我的日食说法,它无法编译:

The method stream(T[]) in the type Arrays is not applicable for the arguments  (float[])

float[]Java 8 中连接两个数组的最有效(最简单)方法是什么?


更新:显然,问题的全部要点是我必须处理float而不是double

Sandip Solanki:

自己不要做,System.arrayCopy()可以将两个数组都复制到合并大小的新数组中。由于它使用本机OS代码,因此效率更高。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章