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可以是同一个数组对象。
本文介绍了一种在Java中高效拼接多个字节数组的方法。通过使用`Arrays.copyOf()`和`System.arraycopy()`来实现数组的复制与拼接,这种方法不仅能够处理不同长度的数组,还能确保当源数组和目标数组相同时正确地进行复制操作。
1075

被折叠的 条评论
为什么被折叠?



