集册 Java实例教程 将多个字节数组合并为一个较长的字节数组。

将多个字节数组合并为一个较长的字节数组。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

401
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将多个字节数组合并为一个较长的字节数组。


//package com.nowjava;

/** 
 来自 NowJava.com**/

public class Main {

    public static void main(String[] argv) throws Exception {

        byte[] a = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };

        System.out.println(java.util.Arrays.toString(combineArrays(a)));

    }


    /**

     * Combines multiple byte arrays into a single, longer byte array. Each byte array is appended to the end of the previous byte

     * array. The arrays do not need to be the same length. An example: [1, 2, 3] and [4, 5] and [6, 7, 8, 9] would return the byte

     * array of [1, 2, 3, 4, 5, 6, 7, 8, 9].

     * @param a - The byte arrays to combine.

     * @return The combined byte array.

     */

    public 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 (int i = 0; i < a.length; i++) 
展开阅读全文