public class Test04_ArrayCopy { // 参数(源数组名,源数级开始点,目标数组名,目标数组开始点,复制的长度) public static void copy(int s[], int s1, int o[], int s2, int len){ for(int i=0; i<len; i++){ o[s2 + i] = s[s1 + i]; // 修改目标数组的内容 } } public static void print(int a[]){ // 数组输出 for(int i=0; i<a.length; i++){ System.out.print(a[i] + " "); }System.out.println(); } public static void main(String[] args){ int i1[] = {1,2,3,4,5,6,7,8,9}; // 源数组 int i2[] = {11,22,33,44,55,66,77,88,99}; // 目标数组 copy(i1, 3, i2, 1, 3); // 自定义方法copy数组 print(i2); System.arraycopy(i1, 3, i2, 1, 3); // Java类库对数组复制的支持 print(i2); } }
输出结果:
11 4 5 6 55 66 77 88 99
11 4 5 6 55 66 77 88 99