java数组叠加问题.有个较大的字节数组 byte[] total=new byte[12]这里有个三个临时的小的字节数组 byte[] a和 byte[] b 以及  byte[] c,每个只有4个字节,java中如何写更简单,效率更好的将三个小的放到total中。

解决方案 »

  1.   

    按照什么规则放入?如果是abc顺序放入的话,直接遍历就ok了byte[][] tmp=new byte[][]{a,b,c};
    for(int i=0;i<12;i++){
        total[i]=tmp[i/4][i%4];
    }
      

  2.   

    遍历赋值就可以了,还可以通过数组拷贝,如
    System.arraycopy(a, 0, total, 0, a.length);
    System.arraycopy(b, 0, total, a.length, b.length);
    System.arraycopy(c, 0, total, a.length+b.length, c.length);
      

  3.   

    其实许多的问题都是有现成的解决方案,有句俗话说 "懒惰是程序员的美德",那就做个"懒惰"的程序员吧;在apache commons 中有个commons-lang.jar包,里面包含了对数组处理的一个工具类ArrayUtils,对数组的处理提供了很多的便利。如你不清楚有多少个byte[] temp,且也担心temp是否为null等,这就很方便了。给个例子:(需要commons-lang-2.6.jar)import org.apache.commons.lang.ArrayUtils;...
           public static void main(String[] args) {
    byte[] total = fill(null, new byte[]{1,2,3,4});
    total = fill(total, new byte[]{5,6,7,8});
    total = fill(total, new byte[]{9,10,11,12}); System.out.println(ArrayUtils.toString(total));
    } public static byte[] fill(byte[] total, byte[] temp){
    return ArrayUtils.addAll(total,temp);
    }
      

  4.   

    public class Myjava { public static void main(String[] args) {
    byte[] total = new byte[12];
    byte[] a = new byte[] { 1, 2, 3, 4 };
    byte[] b = new byte[] { 4, 5, 6, 7 };
    byte[] c = new byte[] { 8, 9, 10, 11 }; System.arraycopy(a, 0, total, 0, 4);
    System.arraycopy(b, 0, total, 4, 4);
    System.arraycopy(c, 0, total, 8, 4);
    }
    }
      

  5.   

    就是System.arrayCopy,这个是用jni实现的,直接copy内存,效率高