Is there an equivalent to memcpy() in Java?

前端 未结 9 1791
灰色年华
灰色年华 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 09:54

    If you just want an exact copy of a one-dimensional array, use clone().

    byte[] array = { 0x0A, 0x01 };
    byte[] copy = array.clone();
    

    For other array copy operations, use System.arrayCopy/Arrays.copyOf as Tom suggests.

    In general, clone should be avoided, but this is an exception to the rule.

提交回复
热议问题