我需要这样一个东西:一个BYTE数组A,它可以将另一个BYTE数组(缓冲数组,大小固定)附加到自己,即A自身长度是可增加的,可以不断合并一个固定大小的BYTE数组。谁知道如何实现?麻烦大家帮帮忙~

解决方案 »

  1.   

    不可以,数组在定义时必须定义长度的,下面的方法可以实现你的要求,仅供参考
    public class Example {
    public static void main(String[] args) {
    for (byte b : byteArrayAdd(new byte[] { 1, 2 }, new byte[] { 3, 4, 5,
    5, 6 }))
    System.out.println(b);
    } public static byte[] byteArrayAdd(byte src[], byte obj[]) {
    byte result[] = new byte[src.length + obj.length];
    int i = 0;
    for (byte b : src) {
    result[i] = b;
    i += 1;
    }
    for (byte b : obj) {
    result[i] = b;
    i += 1;
    }
    return result;
    }
    }
      

  2.   

    把第一个byte 足够大.           或者弄三个数组.  temp 就行了.
      

  3.   

    一个简单实现,无非就是把byte[]包装一下
    public class ByteArray {
    private byte value[];
    private int count; public ByteArray() {
    value = new byte[128];
    } public ByteArray(int capacity) {
    value = new byte[capacity];
    } public int length() {
    return count;
    } public int capacity() {
    return value.length;
    } public void trimToSize() {
    if (count < value.length) {
    value = Arrays.copyOf(value, count);
    }
    } public ByteArray append(byte bytes[]) {
    int newCount = count + bytes.length;
    if (newCount > value.length)
    expandCapacity(newCount);
    System.arraycopy(bytes, 0, value, count, bytes.length);
    count = newCount;
    return this;
    } public ByteArray append(byte bytes[], int offset, int len) {
    int newCount = count + len;
    if (newCount > value.length)
    expandCapacity(newCount);
    System.arraycopy(bytes, offset, value, count, len);
    count = newCount;
    return this;
    } public byte[] toByteArray() {
    byte result[] = new byte[count];
    System.arraycopy(value, 0, result, 0, count);
    return result;
    } public byte byteAt(int index) {
    if ((index < 0) || (index >= count))
        throw new IndexOutOfBoundsException("" + index);
    return value[index];
    } void expandCapacity(int minimumCapacity) {
    int newCapacity = (value.length + 1) * 2;
    if (newCapacity < 0) {
    newCapacity = Integer.MAX_VALUE;
    } else if (minimumCapacity > newCapacity) {
    newCapacity = minimumCapacity;
    }
    value = Arrays.copyOf(value, newCapacity);
    }
    }