Java中如何将两个数组相加呢?
下文笔者讲述使用java代码将两个数组相加组合为一个新的数组的方法分享,如下所示
数组相加的示例分享
JoinArray.java使用stream进行数组连接
JoinArray.java
数组相加的实现思路: 方式1: 使用Apache Commons Lang中的ArrayUtils工具类 方式2: 使用反射进行数组相加 方式3: 使用stream进行数组相加例:
数组相加的示例分享
Apache Commons Lang – ArrayUtils
使用ArrayUtils.addAll即可将两个数组相加,此方法支持基础类型数组和对象类型数组相加JoinArray.java
package com.java265.example.array; import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; public class JoinArray { public static void main(String[] args) { String[] s1 = new String[]{"java265.com-1", "java爱好者", "java最牛"}; String[] s2 = new String[]{"d", "e", "f"}; String[] result = ArrayUtils.addAll(s1, s2); System.out.println(Arrays.toString(result)); int [] int1 = new int[]{88,99,100}; int[] int2 = new int[]{77,222,112}; int[] result2 = ArrayUtils.addAll(int1, int2); System.out.println(Arrays.toString(result2)); } } 注意事项: 需引入Apache commons-lang3这个jar包 pom.xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency>
使用反射进行数组相加
JoinArray.javapackage com.java265.example.array; import java.lang.reflect.Array; import java.util.Arrays; public class JoinArray { public static void main(String[] args) { String[] s1 = new String[]{"java265.com-1", "java爱好者", "java最牛"}; String[] s2 = new String[]{"d", "e", "f"}; String[] s3 = new String[]{"g", "h", "i"}; String[] result = joinArrayGeneric(s1, s2, s3); System.out.println(Arrays.toString(result)); int [] int1 = new int[]{88,99,100}; int[] int2 = new int[]{77,222,112}; int[] int3 = new int[]{7, 8, 9}; int[] result2 = joinArray(int1, int2, int3); System.out.println(Arrays.toString(result2)); } static <T> T[] joinArrayGeneric(T[]... arrays) { int length = 0; for (T[] array : arrays) { length += array.length; } //T[] result = new T[length]; final T[] result = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(), length); int offset = 0; for (T[] array : arrays) { System.arraycopy(array, 0, result, offset, array.length); offset += array.length; } return result; } static int[] joinArray(int[]... arrays) { int length = 0; for (int[] array : arrays) { length += array.length; } final int[] result = new int[length]; int offset = 0; for (int[] array : arrays) { System.arraycopy(array, 0, result, offset, array.length); offset += array.length; } return result; } //create other overloaded primitive type - long, double... //static long[] joinArray(long[]... arrays) }
使用stream进行数组连接
形成一个新的stream
然后再转换为一个新数组
JoinArray.java
package com.java265.example.array; import java.util.Arrays; import java.util.stream.IntStream; import java.util.stream.Stream; public class JoinArray { public static void main(String[] args) { String[] s1 = new String[]{"java265.com-1", "java爱好者", "java最牛"}; String[] s2 = new String[]{"d", "e", "f"}; String[] s3 = new String[]{"g", "h", "i"}; //join object type array String[] result = Stream.of(s1, s2, s3).flatMap(Stream::of).toArray(String[]::new); System.out.println(Arrays.toString(result)); int [] int1 = new int[]{88,99,100}; int[] int2 = new int[]{77,222,112}; int[] int3 = new int[]{7,8,9}; //join 2 primitive type array int[] result2 = IntStream.concat(Arrays.stream(int1), Arrays.stream(int2)).toArray(); //join 3 primitive type array, any better idea? int[] result3 = IntStream.concat(Arrays.stream(int1), IntStream.concat(Arrays.stream(int2), Arrays.stream(int3))).toArray(); System.out.println(Arrays.toString(result2)); System.out.println(Arrays.toString(result3)); } }
从以上的示例代码中 我们可以看出使用Apache Commons Lang工具类中的方式 是一种最简洁明了的方式,笔者建议大家在数组组合时,使用这种方式
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。