java数组拷贝

2019年2月13日23:03:48 发表评论 1,096 views
/**
 * Created by xabcd on 2019/2/13.
 */
public class java_arraycopy
{public static void main(String args[])
{
    int a1[]={1,2,3,4};
    int a2[] = {9,8,7,6,5,4,3,2};
    System.arraycopy(a1,0,a2,0,3);
    System.out.println("数组a1中的内容:");
    for (int i =0;i<a1.length;i++)
        System.out.print(a1[i]+"  ");
    System.out.println();
    System.out.println("数组a2中的内容:");
    for(int b = 0; b<a2.length;b++)
        System.out.print(a2[b]+"  ");
    System.out.print("数组打印完成");

}

}

System. arraycopy( source, 0, dest, 0, x) 语句 的 意思是: 复制 源 数组 从 下标 0 开始 的 x 个 元素 到 目标 数组, 从 目标 数组 的 下标 0 所 对应 的 位置 开始 存取。

 




结果:
数组a1中的内容:
1 2 3 4 
数组a2中的内容:
1 2 3 6 5 4 3 2 数组打印完成

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: