// 使用 System.arraycopy 的示例代码
public class ArrayCopyExample {
public static void main(String[] args) {
// 原始数组
int[] sourceArray = {1, 2, 3, 4, 5};
// 目标数组,长度为原始数组的两倍
int[] destinationArray = new int[sourceArray.length * 2];
// 使用 System.arraycopy 进行数组复制
// 参数说明:
// sourceArray: 源数组
// 0: 源数组的起始位置
// destinationArray: 目标数组
// 0: 目标数组的起始位置
// sourceArray.length: 要复制的元素个数
System.arraycopy(sourceArray, 0, destinationArray, 0, sourceArray.length);
// 打印目标数组的内容
for (int i : destinationArray) {
System.out.print(i + " ");
}
}
}
System.arraycopy 是 Java 中用于高效复制数组的方法。sourceArray):要从中复制元素的数组。0):从源数组的哪个索引开始复制。destinationArray):将元素复制到的目标数组。0):从目标数组的哪个索引开始放置复制的元素。sourceArray.length):指定要复制多少个元素。在这个例子中,我们将 sourceArray 的所有元素复制到了 destinationArray 的前半部分,而 destinationArray 的后半部分保持为默认值(即 0)。
上一篇:java sm4加解密
下一篇:java数据库连接步骤
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站