例如:
ByteBuffer buf1 = {1,2};
ByteBuffer buf2 = {3,4};怎样弄出一个 ByteBuffer, 使它的值为 {1,2,3,4}

解决方案 »

  1.   

    import java.nio.ByteBuffer;
    class test
    {
    public static void main(String[] args)
    {
    byte[] b1 = new byte[]{1,2};
    byte[] b2 = new byte[]{3,4};
    byte[] b = new byte[4];
    ByteBuffer buf1 = ByteBuffer.allocate(2);
    ByteBuffer buf2 = ByteBuffer.allocate(2);
    buf1 = buf1.put(b1);
    b = buf1.array();
    for(int i = 0; i != b.length; i++)
    System.out.println(b[i]);
    buf2 = buf2.put(b2);
    b = buf2.array();
    for(int i = 0; i != b.length; i++)
    System.out.println(b[i]);

    ByteBuffer buf3 = ByteBuffer.allocate(4);
    buf3.put(buf1.array());
    buf3.put(buf2.array());
    b = buf3.array();
    for(int i = 0; i != b.length; i++)
    System.out.println(b[i]);
    }
    }