Java Program to Merge Two String Arrays

In this blog post, we present a Java program that merges two string arrays into a single array. This program utilizes array manipulation techniques and looping constructs to efficiently combine the elements of the arrays. Let's dive into the code and see how it works! 

Java Program to Merge Two String Arrays

import java.util.Arrays;

public class StringArrayMerger {
    public static void main(String[] args) {
        String[] array1 = {"Hello", "World"};
        String[] array2 = {"Java", "Programming"};

        // Merging the arrays
        String[] mergedArray = mergeArrays(array1, array2);

        // Displaying the merged array
        System.out.println("Merged Array: " + Arrays.toString(mergedArray));
    }

    private static String[] mergeArrays(String[] array1, String[] array2) {
        int length1 = array1.length;
        int length2 = array2.length;
        int mergedLength = length1 + length2;

        String[] mergedArray = new String[mergedLength];

        // Copying elements from array1
        System.arraycopy(array1, 0, mergedArray, 0, length1);

        // Copying elements from array2
        System.arraycopy(array2, 0, mergedArray, length1, length2);

        return mergedArray;
    }
}

Output:

Merged Array: [Hello, World, Java, Programming]

Explanation: 

1. The program starts by initializing two string arrays: array1 and array2, containing the desired elements.

2. The mergeArrays() method takes in the two arrays as parameters and returns a new array that contains the merged elements. 

3. Inside the mergeArrays() method, we calculate the lengths of array1 and array2 and determine the total length of the merged array. 

4. A new string array called mergedArray is created with a length equal to the sum of the lengths of array1 and array2. 

5. The System.arraycopy() method is used to copy the elements from array1 to mergedArray. The parameters are the source array, the starting position in the source array, the destination array, the starting position in the destination array, and the number of elements to copy. 

6. Next, the elements from array2 are copied to mergedArray using System.arraycopy()

7. Finally, the mergedArray is returned from the mergeArrays() method. 

8. The program prints the merged array using Arrays.toString() to display the contents of the array. 

Feel free to modify the array1 and array2 variables with your own arrays to test the program with different inputs. 

Conclusion

Congratulations! You have learned how to write a Java program to merge two string arrays into a single array. By utilizing array manipulation techniques and the System.arraycopy() method, we efficiently combine the elements of the arrays. This program can be useful when dealing with data from multiple sources or when merging datasets. 

Feel free to incorporate this code into your Java projects or customize it to suit your specific requirements. Happy coding!

Related Java String Programs with Output

Comments

  1. Hello Ramesh,

    I have been following Java guides and learnt a lot from your site. I would really like to thank you.

    In this program, I think we can also use Hashset and add all the elements from both the arrays and can convert to array.

    I will share my code here, please review and let me know if anything is wrong.

    public static String[] mergeArrays(String[] strArr1, String[] strArr2){
    if(strArr1 == null || strArr1.length == 0){
    return strArr2;
    }
    if(strArr2 == null || strArr2.length == 0){
    return strArr1;
    }
    LinkedHashSet set = new LinkedHashSet<>();
    for (String s : strArr1) {
    set.add(s);
    }
    for (String s : strArr2) {
    set.add(s);
    }
    System.out.println(set);



    return (String []) set.toArray(new String[set.size()]);
    }


    Thank you!

    ReplyDelete
    Replies
    1. I also tried other way, please check this as well. Thank you

      public static String[] mergeWithoutFor(String[] strArr1, String[] strArr2){

      HashSet set = new HashSet(Arrays.asList(strArr1));
      set.addAll(Arrays.asList(strArr2));

      System.out.println(set);
      return (String[]) set.toArray(new String[set.size()]);
      }

      Delete

Post a Comment

Leave Comment