连接两个int [] [英] Concatenating two int[]
问题描述
通过<$ c $连接两个 String []
或 Integer []
的简单解决方案C>流。由于经常使用 int []
。是否有任何直接的方法来连接两个 int []
?
There are easy solutions for concatenating two String[]
or Integer[]
in java by Streams
. Since int[]
is frequently used. Is there any straightforward way for concatenating two int[]
?
以下是我的想法:
int[] c = {1, 34};
int[] d = {3, 1, 5};
Integer[] cc = IntStream.of(c).boxed().toArray(Integer[]::new);
Integer[] dd = Arrays.stream(d).boxed().toArray(Integer[]::new);
int[] m = Stream.concat(Stream.of(cc), Stream.of(dd)).mapToInt(Integer::intValue).toArray();
System.out.println(Arrays.toString(m));
>>
[1, 34, 3, 1, 5]
它有效,但实际上将 int []
转换为 Integer []
,然后转换 Integer []
再次返回 int []
。
It works, but it actually converts int[]
to Integer[]
, then converts Integer[]
back to int[]
again.
推荐答案
您可以与<$ c一起使用 IntStream.concat
$ c> Arrays.stream 在没有任何自动装箱或拆箱的情况下完成此操作。以下是它的外观。
You can use IntStream.concat
in concert with Arrays.stream
to get this thing done without any auto-boxing or unboxing. Here's how it looks.
int[] result = IntStream.concat(Arrays.stream(c), Arrays.stream(d)).toArray();
请注意 Arrays.stream(c)
返回 IntStream
,然后在收集到数组之前与其他 IntStream
连接。
Note that Arrays.stream(c)
returns an IntStream
, which is then concatenated with the other IntStream
before collected into an array.
这是输出。
[1,34,3,1,5]
[1, 34, 3, 1, 5]
这篇关于连接两个int []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!