How to Join Two Arrays in Java


In this example we will show the method to join two Arrays in Java.

Source Code

package com.beginner.examples;

import java.util.Arrays;

public class JoinArray {
  public static void main(String[] args) {
    String[] str1 = {"a", "b", "c"};
    String[] str2 = {"x", "y", "z"};
    String[] str3 = new String[str1.length + str2.length];
    // join two Arrays
    System.arraycopy(str1, 0, str3, 0, str1.length); 
    System.arraycopy(str2, 0, str3, str1.length, str2.length);
    System.out.println(Arrays.toString(str3));
  }
}

Output:

[a, b, c, x, y, z]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments