1

What is the simple method two concatenate two byte arrays in Java? I used this function but got an error:

java.lang.ArrayIndexOutOfBoundsException: 16

My function is:

public static byte[] concatinate(byte[] a, byte[] b) {
    byte[] c = new byte[100];
    for (int i = 0; i < (a.length + b.length); i++) {
        if (i < a.length) {
            c[i] = a[i];
        } else {
            c[i] = b[i];
        }
    }
    return c;
}
1
  • 1
    Here, i > b.length, will cause this exception. Commented Jul 10, 2015 at 8:47

3 Answers 3

5

Firstly, your code is bound to fail if a.length + b.length > 100. You should be using a.length + b.length as the length of c.

Yes, because when you've gone past a.length, you're still trying to use b[i]. Imagine a.length is 50 and b.length is 1. You've got 51 array elements to populate, but to populate c[50] you need b[0], not b[50].

All you need to change is this:

c[i] = b[i];

To this:

c[i] = b[i - a.length];

... or have two loops instead, as per Mureinik's answer. (I wouldn't expect either option to be significantly faster than the other, and they're definitely equivalent - you can use whichever you find most readable.)

However, I'd recommend using System.arraycopy instead:

public static byte[] concatenate(byte[] a, byte[] b) {
    byte[] c = new byte[a.length + b.length];
    System.arraycopy(a, 0, c, 0, a.length);
    System.arraycopy(b, 0, c, a.length, b.length);
    return c;
}

Much simpler :)

Sign up to request clarification or add additional context in comments.

17 Comments

HOW WE CAN USE System.arraycopy ?
@AjmalMuhammad: a) did you try before asking? It's not terribly hard... b) there's an example in my answer
@Jon c[i] = b[i - a.length]; is wrong you will miss some elements it is in if else
@raghavendra: Which elements will it miss? If i is less than a.length, it'll copy the value from a. If i is a.length or greater, it'll copy the value from b. A total of a.length + b.length elements are copied, which is correct. Where's the problem?
@raghavendra: Yes, indeed. So when i == a.length, then b[i - a.length] is b[0]. I suggest you work this through on a piece of paper with an example. It really is fine...
|
1

Just define an array the size of the combined two arrays, and iterate each of them individually:

public static byte[] concatinate(byte[] a, byte[] b) {
    byte[] c = new byte[a.length + b.length];
    for (int i = 0; i < a.length; i++) {
        c[i] = a[i];
    }

    for (int i = 0; i < b.length; i++) {
        c[a.length + i] = b[i];
    }

    return c;
}

Comments

1

The code for appending 2 byte[] was good.If you want to keep on appending the byte[] you can use this code....

byte[] readBytes ; // Your byte array .... //for eg. readBytes = "TestBytes".getBytes();

ByteArrayBuffer mReadBuffer = new ByteArrayBuffer(0 ) ; // Instead of 0, if you know the count of expected number of bytes, nice to input here

mReadBuffer.append(readBytes, 0, readBytes.length); // this copies all bytes from readBytes byte array into mReadBuffer // Any new entry of readBytes, you can just append here by repeating the same call.

// Finally, if you want the result into byte[] form: byte[] result = mReadBuffer.buffer();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.