Java Merge Byte Arrays

Java Merge Byte Arrays | The byte arrays are arrays of byte data type values. The byte is one of the eight primitive data types that the Java programming language supports. It ranges from -128 to 127, and its size is 8 bit. A byte is an 8-bit signed two’s complement integer. In this section, we will see how to merge two byte arrays in java. Also see:- Convert ArrayList to Byte Array Java

For more understanding let us see some examples:-

1) byte array1[ ] = {10, 20, 30}
byte array2[ ] = {15, 25, 35}
After merge array = {10, 20, 30, 15, 25, 35}

2) byte array1[ ] = {-1, 12, -30}
byte array2[ ] = {-5, -2, -35}
After merge array = {-1, 12, -30, -5, -2, -35}

Also see:- Merge 2 Arrays in Java, Merge two sorted Arrays

Java Merge Byte Arrays Using System.arraycopy() Method

The method details of System.arraycopy() method is as follows:-
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

Parameters in this method are:-
src:- The source array.
srcPos:- Starting position in the source array.
dest:- The destination array.
destPos:- starting position in the destination array.
length:- the number of array elements to be copied.

How to merge two byte arrays in Java using System.arraycopy() Method

import java.util.Arrays;

public class Main {

   public static void main(String[] args) {      
      byte array1[] = {15, 2, 40, 40, 20};
      byte array2[] = {91, 11, 29, 33, 65};
      byte resultArray[] = new byte[array1.length + array2.length];
      
      // add array1 to resultArray
      System.arraycopy(array1, 0, resultArray, 0, array1.length);
      
      // add array2 to resultArray  
      System.arraycopy(array2, 0, resultArray, 
                       array1.length, array2.length);
      
      System.out.println("Array1 = " + Arrays.toString(array1));
      System.out.println("Array2 = " + Arrays.toString(array2));
      System.out.println("Merged Array = " 
                             + Arrays.toString(resultArray));
   }
}

Output:-

Array1 = [15, 2, 40, 40, 20]
Array2 = [91, 11, 29, 33, 65]
Merged Array = [15, 2, 40, 40, 20, 91, 11, 29, 33, 65]

How to append to byte array in Java Using Loops

import java.util.Arrays;

public class Main {

   public static void main(String[] args) {
      byte array1[] = { 1, 2, 3, 4, 5 };
      byte array2[] = { 9, 8, 7, 6, 5 };
      byte resultArray[] = new byte[array1.length + array2.length];

      for (int i = 0; i < array1.length; i++) {
         resultArray[i] = array1[i];
      }

      for (int i = 0, j = array1.length; 
              j < (array1.length + array2.length); j++, i++) {
         resultArray[j] = array2[i];
      }

      System.out.println("Array1 = " + Arrays.toString(array1));
      System.out.println("Array2 = " + Arrays.toString(array2));
      System.out.println("Merged Array = " + 
                         Arrays.toString(resultArray));
   }
}

Output:-

Array1 = [1, 2, 3, 4, 5]
Array2 = [9, 8, 7, 6, 5]
Merged Array = [1, 2, 3, 4, 5, 9, 8, 7, 6, 5]

Compared to the previous concatenate byte arrays Java program using System.arraycopy() the above program uses for loop. We have declared the third array of size of the sum of the existing two arrays. The first array is copied to the new array and after that second array is also copied to the new array.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *