Arrays.copyOf(dataType[] srcArray,int length);其中,srcArray 表示要进行复制的数组,length 表示复制后的新数组的长度。
import java.util.Arrays;
public class Test19{
public static void main(String[] args) {
// 定义长度为 5 的数组
int scores[] = new int[]{57,81,68,75,91};
// 输出原数组
System.out.println("原数组内容如下:");
// 循环遍历原数组
for(int i=0;i<scores.length;i++) {
// 将数组元素输出
System.out.print(scores[i]+"\t");
}
// 定义一个新的数组,将 scores 数组中的 5 个元素复制过来
// 同时留 3 个内存空间供以后开发使用
int[] newScores = (int[])Arrays.copyOf(scores,8);
System.out.println("\n复制的新数组内容如下:");
// 循环遍历复制后的新数组
for(int j=0;j<newScores.length;j++) {
// 将新数组的元素输出
System.out.print(newScores[j]+"\t");
}
}
}
在上述代码中,由于原数组 scores 的长度为 5,而要复制的新数组 newScores 的长度为 8,因此在将原数组中的 5 个元素复制完之后,会采用默认值填充剩余 3 个元素的内容。原数组内容如下: 57 81 68 75 91 复制的新数组内容如下: 57 81 68 75 91 0 0 0
Arrays.copyOfRange(dataType[] srcArray,int startIndex,int endIndex)其中:
public class Test20 {
public static void main(String[] args) {
// 定义长度为8的数组
int scores[] = new int[] { 57, 81, 68, 75, 91, 66, 75, 84 };
System.out.println("原数组内容如下:");
// 循环遍历原数组
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + "\t");
}
// 复制原数组的前5个元素到newScores数组中
int newScores[] = (int[]) Arrays.copyOfRange(scores, 0, 5);
System.out.println("\n复制的新数组内容如下:");
// 循环遍历目标数组,即复制后的新数组
for (int j = 0; j < newScores.length; j++) {
System.out.print(newScores[j] + "\t");
}
}
}
在上述代码中,原数组 scores 中包含有 8 个元素,使用 Arrays.copyOfRange() 方法可以将该数组复制到长度为 5 的 newScores 数组中,截取 scores 数组的前 5 个元素即可。原数组内容如下: 57 81 68 75 91 66 75 84 复制的新数组内容如下: 57 81 68 75 91
System.arraycopy(dataType[] srcArray,int srcIndex,int destArray,int destIndex,int length)其中,srcArray 表示原数组;srcIndex 表示原数组中的起始索引;destArray 表示目标数组;destIndex 表示目标数组中的起始索引;length 表示要复制的数组长度。
public class Test21 {
public static void main(String[] args) {
// 定义原数组,长度为8
int scores[] = new int[] { 100, 81, 68, 75, 91, 66, 75, 100 };
// 定义目标数组
int newScores[] = new int[] { 80, 82, 71, 92, 68, 71, 87, 88, 81, 79, 90, 77 };
System.out.println("原数组中的内容如下:");
// 遍历原数组
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + "\t");
}
System.out.println("\n目标数组中的内容如下:");
// 遍历目标数组
for (int j = 0; j < newScores.length; j++) {
System.out.print(newScores[j] + "\t");
}
System.arraycopy(scores, 0, newScores, 2, 8);
// 复制原数组中的一部分到目标数组中
System.out.println("\n替换元素后的目标数组内容如下:");
// 循环遍历替换后的数组
for (int k = 0; k < newScores.length; k++) {
System.out.print(newScores[k] + "\t");
}
}
}
在该程序中,首先定义了一个包含有 8 个元素的 scores 数组,接着又定义了一个包含有 12 个元素的 newScores 数组,然后使用 for 循环分别遍历这两个数组,输出数组中的元素。最后使用 System.arraycopy() 方法将 newScores 数组中从第三个元素开始往后的 8 个元素替换为 scores 数组中的 8 个元素值。原数组中的内容如下: 100 81 68 75 91 66 75 100 目标数组中的内容如下: 80 82 71 92 68 71 87 88 81 79 90 77 替换元素后的目标数组内容如下: 80 82 100 81 68 75 91 66 75 100 90 77
array_name.clone()
int[] targetArray=(int[])sourceArray.clone();
public class Test22 {
public static void main(String[] args) {
// 定义原数组,长度为8
int scores[] = new int[] { 100, 81, 68, 75, 91, 66, 75, 100 };
System.out.println("原数组中的内容如下:");
// 遍历原数组
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + "\t");
}
// 复制数组,将Object类型强制转换为int[]类型
int newScores[] = (int[]) scores.clone();
System.out.println("\n目标数组内容如下:");
// 循环遍历目标数组
for (int k = 0; k < newScores.length; k++) {
System.out.print(newScores[k] + "\t");
}
}
}
在该程序中,首先定义了一个长度为 8 的 scores 数组,并循环遍历该数组输出数组中的元素,然后定义了一个名称为 newScores 的新数组,并使用 scores.clone() 方法将 scores 数组中的元素复制给 newScores 数组。最后循环遍历 newScores 数组,输出数组元素。原数组中的内容如下: 100 81 68 75 91 66 75 100 目标数组内容如下: 100 81 68 75 91 66 75 100从运行的结果可以看出,scores 数组的元素与 newScores 数组的元素是相同的。
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有