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
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Swap Two Strings Without Using Third Variable
Hello Ramesh,
ReplyDeleteI 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!
I also tried other way, please check this as well. Thank you
Deletepublic 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()]);
}