• java System.arrayCopy使用说明


    java System.arrayCopy使用说明

    java.lang.System.arraycopy() 方法复制指定的源数组的数组,在指定的位置开始,到目标数组的指定位置。

    下面是 System.arrayCopy的源代码声明 : 

    public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
    代码解释:
      Object src : 原数组
    int srcPos : 从元数据的起始位置开始
      Object dest : 目标数组
      int destPos : 目标数组的开始起始位置
      int length : 要copy的数组的长度

    比如 :我们有一个数组数据 byte[]  srcBytes = new byte[]{2,4,0,0,0,0,0,10,15,50};  // 源数组

                                        byte[] destBytes = new byte[5]; // 目标数组

    我们使用System.arraycopy进行转换(copy)

    System.arrayCopy(srcBytes,0,destBytes ,0,5)
    上面这段代码就是 : 创建一个一维空数组,数组的总长度为 12位,然后将srcBytes源数组中 从0位 到 第5位之间的数值 copy 到 destBytes目标数组中,在目标数组的第0位开始放置.
    那么这行代码的运行效果应该是 2,4,0,0,0,
    我们来运行一下
    1         byte[]  srcBytes = new byte[]{2,4,0,0,0,0,0,10,15,50};
    2         byte[] destBytes = new byte[5];
    3         System.arraycopy(srcBytes, 0, destBytes, 0, 5); 
    4         for(int i = 0;i< destBytes.length;i++){
    5             System.out.print("-> " + destBytes[i]);
    6         } 

    运行结果  : -> 2-> 4-> 0-> 0-> 0

  • 相关阅读:
    zoj3814
    cf249D
    codeforces 461C
    uva 11584
    Codeforces Round #247 (Div. 2) C D
    AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 6. Mathematical Concepts and Methods
    AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) Chapter 3. Data Structures Fundamental Data Structures
    Codeforces Round #257 (Div. 2)
    DAY 16 PYTHON入门
    DAY 15 PYTHON入门
  • 原文地址:https://www.cnblogs.com/DeepLearing/p/5688555.html
Copyright © 2020-2023  润新知