本文目录导读:

- 方法一:使用System.arraycopy()(推荐)
- 方法二:使用循环遍历
- 方法三:使用Java 8+ Stream API
- 方法四:通用工具方法
- 方法五:使用Apache Commons Lang(第三方库)
- 性能比较与推荐
- 实践建议
在Java中合并两个数组有多种方法,我来为你详细介绍几种常用且实用的方式:
使用System.arraycopy()(推荐)
这是最高效的方法,适用于所有场景:
public class MergeArrayExample {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};
// 创建新数组,长度为两个数组长度之和
int[] mergedArray = new int[arr1.length + arr2.length];
// 复制第一个数组
System.arraycopy(arr1, 0, mergedArray, 0, arr1.length);
// 复制第二个数组
System.arraycopy(arr2, 0, mergedArray, arr1.length, arr2.length);
// 输出结果
System.out.println("合并后的数组:" + Arrays.toString(mergedArray));
// 输出:[1, 2, 3, 4, 5, 6, 7, 8]
}
}
使用循环遍历
最基础的方法,适合理解合并原理:
public class MergeArrayExample {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};
// 创建新数组
int[] mergedArray = new int[arr1.length + arr2.length];
// 使用循环复制第一个数组
for (int i = 0; i < arr1.length; i++) {
mergedArray[i] = arr1[i];
}
// 使用循环复制第二个数组
for (int i = 0; i < arr2.length; i++) {
mergedArray[arr1.length + i] = arr2[i];
}
System.out.println("合并后的数组:" + Arrays.toString(mergedArray));
}
}
使用Java 8+ Stream API
简洁现代的写法,适合函数式编程风格:
import java.util.stream.IntStream;
import java.util.Arrays;
public class MergeArrayExample {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};
// 使用Stream合并数组
int[] mergedArray = IntStream.concat(
Arrays.stream(arr1),
Arrays.stream(arr2)
).toArray();
System.out.println("合并后的数组:" + Arrays.toString(mergedArray));
}
}
通用工具方法
创建可复用的通用方法,支持任意类型数组:
import java.util.Arrays;
public class ArrayMergeUtil {
// 合并int数组
public static int[] merge(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length + arr2.length];
System.arraycopy(arr1, 0, result, 0, arr1.length);
System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
return result;
}
// 通用方法,合并任意类型数组
public static <T> T[] merge(T[] arr1, T[] arr2) {
T[] result = Arrays.copyOf(arr1, arr1.length + arr2.length);
System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
return result;
}
public static void main(String[] args) {
// 测试int数组
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] merged = merge(arr1, arr2);
System.out.println("int数组合并:" + Arrays.toString(merged));
// 测试String数组
String[] strArr1 = {"Hello", "World"};
String[] strArr2 = {"Java", "Programming"};
String[] strMerged = merge(strArr1, strArr2);
System.out.println("String数组合并:" + Arrays.toString(strMerged));
}
}
使用Apache Commons Lang(第三方库)
如果你的项目使用了Apache Commons Lang库:
import org.apache.commons.lang3.ArrayUtils;
public class MergeArrayExample {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {5, 6, 7, 8};
// 使用ArrayUtils合并数组
int[] mergedArray = ArrayUtils.addAll(arr1, arr2);
System.out.println("合并后的数组:" + Arrays.toString(mergedArray));
}
}
性能比较与推荐
| 方法 | 性能 | 代码简洁度 | 适用场景 |
|---|---|---|---|
System.arraycopy() |
最高 | 中等 | 通用场景,推荐使用 |
| 循环遍历 | 较低 | 较低 | 理解原理,简单场景 |
| Stream API | 中等 | 高 | Java 8+,函数式编程 |
| Apache Commons | 较高 | 最高 | 项目已包含该库 |
实践建议
- 日常开发:推荐使用
System.arraycopy()或工具方法 - 学习理解:可以用循环遍历方法理解原理
- 追求简洁:可以使用Stream API
- 项目规范:建议封装成工具类,提高代码复用性
选择哪种方法取决于你的具体需求和项目环境。