合并byte数组

假如有这么几个byte[],想要把它们合成一个byte[]。

1             byte[] bytes1 = { 1, 2, 3, 4, 5 };
2             byte[] bytes2 = { 6, 7, 8, 9 };
3             byte[] bytes3 = { 10, 11, 12, 13, 14, 15 };

一般我们会这么写:

1             byte[] allBytes1 = new byte[bytes1.Length + bytes2.Length + bytes3.Length];
2             bytes1.CopyTo(allBytes1, 0);
3             bytes2.CopyTo(allBytes1, bytes1.Length);
4             bytes3.CopyTo(allBytes1, bytes1.Length + bytes2.Length);
5             foreach (byte item in allBytes1)
6             {
7                 Console.WriteLine(item);
8             }

不过,这样感觉好麻烦,如果要合并的byte[]太多,CopyTo()会调用你想吐,第二个参数的值也会让你+Length加到你郁闷至死,怎么办?写个方法吧。调用这一个ConcatBytes方法就行了,参数是动态的,可以放无数个。

            // 合并byte[]
            byte[] allBytes2 = ConcatBytes(bytes1, bytes2, bytes3);
            foreach (byte item in allBytes2)
            {
                Console.WriteLine(item);
            }

        /// <summary>
        /// 合并byte数组
        /// </summary>
        /// <param name="sourceBytesArray">要合并的数组集合</param>
        /// <returns>合并后的byte数组</returns>
        private byte[] ConcatBytes(params byte[][] sourceBytesArray)
        {
            int allLength = sourceBytesArray.Sum(o => o.Length);
            byte[] resultBytes = new byte[allLength];
            for (int i = 0; i < sourceBytesArray.Length; i++)
            {
                int copyToIndex = GetCopyToIndex(sourceBytesArray, i);
                sourceBytesArray[i].CopyTo(resultBytes, copyToIndex);
            }
            return resultBytes;
        }

        /// <summary>
        /// 获取复制开始处的索引
        /// </summary>
        /// <param name="sourceBytesArray">byte[]的所在数组</param>
        /// <param name="index">byte[]的所在数组的索引</param>
        /// <returns>复制开始处的索引</returns>
        private int GetCopyToIndex(byte[][] sourceBytesArray, int index)
        {
            if (index == 0)
            {
                return 0;
            }
            return sourceBytesArray[index - 1].Length + GetCopyToIndex(sourceBytesArray, index - 1);
        }

其实,用Framework提供的方法感觉更简单

1             List<byte> sendByteList = new List<byte>();
2             sendByteList.AddRange(bytes1);
3             sendByteList.AddRange(bytes2);
4             sendByteList.AddRange(bytes3);
5             byte[] allBytes3 = sendByteList.ToArray();
6             foreach (byte item in allBytes3)
7             {
8                 Console.WriteLine(item);
9             }

 


  

  

转载于:https://www.cnblogs.com/yuwuji/p/8081897.html

### 如何在编程中合并 `byte` 数组 在 Java 编程中,可以通过多种方式实现 `byte` 数组合并。以下是几种常见的方法及其对应的示例代码。 #### 方法一:手动复制法 这是最基础也是最常见的方法之一。通过创建一个新的更大的数组并将原始数组的内容依次拷贝到新数组中完成合并操作。 ```java public static byte[] mergeByteArrays(byte[] array1, byte[] array2) { int length = array1.length + array2.length; byte[] result = new byte[length]; System.arraycopy(array1, 0, result, 0, array1.length); System.arraycopy(array2, 0, result, array1.length, array2.length); return result; } ``` 这种方法利用了 `System.arraycopy()` 函数高效地完成了数据迁移工作[^1]。 --- #### 方法二:使用 Stream API (适用于较现代版本的 JDK) 对于支持 Streams 的 JDK 版本(JDK 8 及以上),可以借助流式计算简化逻辑: ```java import java.util.stream.IntStream; public static byte[] mergeWithStreams(byte[] array1, byte[] array2) { return IntStream.concat( IntStream.of(array1), IntStream.of(array2)) .toArray(); } ``` 需要注意的是此方法会先将原生类型的数组转为封装类型再进行拼接,因此性能上可能不如传统循环效率高[^3]。 --- #### 方法三:基于第三方库 Apache Commons Lang 工具包 如果项目允许引入外部依赖项,则可以直接调用现成函数减少自定义编码量: ```xml <!-- Maven Dependency --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> ``` 之后就可以这样写: ```java import org.apache.commons.lang3.ArrayUtils; public static byte[] mergeUsingApacheCommons(byte[] array1, byte[] array2){ return ArrayUtils.addAll(array1,array2); } ``` 这种方式不仅简洁明了而且经过大量测试验证其稳定性良好。 --- #### 注意事项 无论采用哪种方案,在实际开发过程中都需要考虑边界条件比如输入参数为空等情况下的异常处理机制设计合理与否会影响最终程序健壮程度。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值