自定义方法copy数组 和 Java类库对数组复制的支持

来源:互联网 发布:阿尔法猎手 软件 编辑:程序博客网 时间:2024/06/06 01:51
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