1. 使用`Arrays.copyOf()`方法:
int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};
int[] result = Arrays.copyOf(array1, array1.length + array2.length);
System.arraycopy(array1, 0, result, 0, array1.length);
System.arraycopy(array2, 0, result, array1.length, array2.length);
// 输出结果
for (int i : result) {
System.out.print(i + " ");
}
2. 使用`System.arraycopy()`方法:
int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};
int[] result = new int[array1.length + array2.length];
System.arraycopy(array1, 0, result, 0, array1.length);
System.arraycopy(array2, 0, result, array1.length, array2.length);
// 输出结果
for (int i : result) {
System.out.print(i + " ");
}
3. 使用Java 8的流(Stream)API:
int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};
int[] result = Stream.of(array1, array2)
.flatMapToInt(Arrays::stream)
.toArray();
// 输出结果
for (int i : result) {
System.out.print(i + " ");
}
以上方法都可以用来拼接数组。选择哪一种方法取决于你的具体需求和偏好