Arrays.copyOf(dataType[] srcArray,int length);其中,srcArray 表示要进行复制的数组,length 表示复制后的新数组的长度。
- import java.util.Arrays;
- public class Testl9
- {
- 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");
- }
- }
- }
import java.util.Arrays; public class Testl9 { 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 个元素的内容。
因为源数组 scores 的数据类型为 int,而使用 Arrays.copyOf(scores,8) 方法复制数组之后返回的是 Object[] 类型,因此需要将 Object[] 数据类型强制转换为 int[] 类型。同时,也正因为 scores 的数据类型为 int,因此默认值为 0。
运行的结果如下所示。
源数组内容如下: 57 81 68 75 91 复制的新数组内容如下: 57 81 68 75 91 0 0 0
Arrays.copyOfRange(dataType[] srcArray,int startIndex,int endIndex)
- import java. util. Arrays;
- 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");
- }
- }
import java. util. Arrays; 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)
- import java.util.Arrays;
- 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");
- }
- }
- }
import java.util.Arrays; 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();
int[] targetArray=(int[])sourceArray.clone();
- import java.util.Arrays;
- 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");
- }
- }
- }
import java.util.Arrays; 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
转载自: http://c.biancheng.net/view/924.html