Java 数组复制技术:全面指南
Java 数组复制技术
学习如何在 Java 中复制一维和二维数组:赋值操作符、循环、System.arraycopy、Arrays.copyOfRange 等,含清晰示例和最佳实践建议。
1. 赋值操作符复制
将一个数组变量赋给另一个是最简单的方法。它复制引用,而非数据。
class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6};
int[] positiveNumbers = numbers; // 引用复制
for (int number: positiveNumbers) {
System.out.print(number + ", ");
}
}
}
输出:
1, 2, 3, 4, 5, 6
numbers 和 positiveNumbers 现在指向同一数组对象。修改一个元素影响另一个:
class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6};
int[] positiveNumbers = numbers;
numbers[0] = -1;
for (int number: positiveNumbers) {
System.out.print(number + ", ");
}
}
}
输出:
-1, 2, 3, 4, 5, 6
这是浅拷贝。为避免副作用,请使用深拷贝技术。
2. 循环深拷贝
遍历每个元素赋值给新数组可创建独立副本。
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[] source = {1, 2, 3, 4, 5, 6};
int[] destination = new int[source.length];
for (int i = 0; i < source.length; ++i) {
destination[i] = source[i];
}
System.out.println(Arrays.toString(destination));
}
}
输出:
[1, 2, 3, 4, 5, 6]
因 source 和 destination 是不同对象,修改一方不影响另一方。
3. 使用 System.arraycopy()
Java 的 System.arraycopy 方法提供快速、内置的数组片段复制方式。
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src– 源数组srcPos– 源起始索引dest– 目标数组destPos– 目标起始索引length– 复制元素数
示例:
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[] n1 = {2, 3, 12, 4, 12, -2};
int[] n2 = new int[n1.length];
System.arraycopy(n1, 0, n2, 0, n1.length);
System.out.println("n2 = " + Arrays.toString(n2));
int[] n3 = new int[5];
System.arraycopy(n1, 2, n3, 1, 2);
System.out.println("n3 = " + Arrays.toString(n3));
}
}
输出:
n2 = [2, 3, 12, 4, 12, -2] n3 = [0, 12, 4, 0, 0]
4. 使用 Arrays.copyOfRange() 复制
需包含特定切片的新数组时,Arrays.copyOfRange 很理想。
import java.util.Arrays;
class ArraysCopy {
public static void main(String[] args) {
int[] source = {2, 3, 12, 4, 12, -2};
int[] destination1 = Arrays.copyOfRange(source, 0, source.length);
System.out.println("destination1 = " + Arrays.toString(destination1));
int[] destination2 = Arrays.copyOfRange(source, 2, 5);
System.out.println("destination2 = " + Arrays.toString(destination2));
}
}
输出:
destination1 = [2, 3, 12, 4, 12, -2] destination2 = [12, 4, 12]
5. 循环复制二维数组
深拷贝二维数组需逐行复制。
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[][] source = {
{1, 2, 3, 4},
{5, 6},
{0, 2, 42, -4, 5}
};
int[][] destination = new int[source.length][];
for (int i = 0; i < destination.length; ++i) {
destination[i] = new int[source[i].length];
for (int j = 0; j < destination[i].length; ++j) {
destination[i][j] = source[i][j];
}
}
System.out.println(Arrays.deepToString(destination));
}
}
输出:
[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]
6. 用 System.arraycopy() 复制二维数组
用 arraycopy 替换内层循环代码更简洁。
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[][] source = {
{1, 2, 3, 4},
{5, 6},
{0, 2, 42, -4, 5}
};
int[][] destination = new int[source.length][];
for (int i = 0; i < source.length; ++i) {
destination[i] = new int[source[i].length];
System.arraycopy(source[i], 0, destination[i], 0, destination[i].length);
}
System.out.println(Arrays.deepToString(destination));
}
}
输出:
[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]
java
- 掌握 C# 数组:声明、初始化与核心操作完全指南
- C++ 数组完全指南:声明、初始化、多维数组与指针技巧
- C++ 动态数组分配实战指南:new/delete、初始化、扩容与内存管理
- 精通 Java 运算符:类型、语法与实战示例
- Java 注释:类型、用法与最佳实践
- 精通 Java 接口:概念、实现与最佳实践
- 精通 Java Try-With-Resources:自动资源管理详解
- Java 注解详解:类型、放置位置与实战示例
- Java 数组教程:声明、创建、初始化与示例代码
- Java 对象数组创建:分步指南
- Java 冒泡排序详解:核心原理、完整代码与步骤演示
- Java 选择排序详解:核心原理、完整代码与步骤演示