Is there an equivalent to memcpy() in Java?

前端 未结 9 1802
灰色年华
灰色年华 2020-12-25 09:34

I have a byte[] and would like to copy it into another byte[]. Maybe I am showing my simple \'C\' background here, but is there an equivalent to memcpy() on byte arrays in

9条回答
  •  情歌与酒
    2020-12-25 10:03

    You might try System.arraycopy or make use of array functions in the Arrays class like java.util.Arrays.copyOf. Both should give you native performance under the hood.

    Arrays.copyOf is probably favourable for readability, but was only introduced in java 1.6.

     byte[] src = {1, 2, 3, 4};
     byte[] dst = Arrays.copyOf(src, src.length);
     System.out.println(Arrays.toString(dst));
    

提交回复
热议问题