concatenate two byte arrays

来源:互联网 发布:硕士双录取 知乎 编辑:程序博客网 时间:2024/06/10 00:58
public static byte[] ConcatArrays(byte[] first, byte[]... rest) {    int totalLength = first.length;    for (byte[] array : rest) {        totalLength += array.length;    }    byte[] result = Arrays.copyOf(first, totalLength);    int offset = first.length;    for (byte[] array : rest) {        System.arraycopy(array, 0, result, offset, array.length);        offset += array.length;    }    return result;}

这里利用Arrays.copyOf() 和 System.arraycopy()

public static void arraycopy (Object src, int srcPos, Object dst, int dstPos, int length)

The source and destination arrays can be the same array, in which case copying is performed as if the source elements are first copied into a temporary array and then into the destination array.
这段的大概意思是,src和dst可以是同一个数组对象。

0 0
原创粉丝点击