1.简介
Java 怎么将多个 byte 数组合并为一个更长的 byte 数组?——请看下文。
2.示例代码
/**
* 版权所有 编程十万个怎么办(www.tah1986.com)
*/
public class CombinesMultipleByteArrays {
public static void main(String[] argv) {
byte[] a = new byte[] { 34, 35, 36, 37, 37};
byte[] b = new byte[] { 37, 67, 68, 69 };
System.out.println("byte 数组a:"+Arrays.toString(a));
System.out.println("byte 数组b:"+Arrays.toString(a));
System.out.println("byte 数组 a 跟 b合并后:"+Arrays.toString(combineArrays(a,b)));
}
private static byte[] combineArrays(byte[]... a) {
int massLength = 0;
for (byte[] b : a) {
massLength += b.length;
}
byte[] c = new byte[massLength];
byte[] d;
int index = 0;
for (byte[] anA : a) {
d = anA;
System.arraycopy(d, 0, c, 0 + index, d.length);
index += d.length;
}
return c;
}
}