Introduction
Merging two arrays involves combining all elements of both arrays into a single array. This task is common in scenarios where you want to consolidate data from different sources. This guide will walk you through writing a Java program that merges two arrays into a single array.
Problem Statement
Create a Java program that:
- Prompts the user to enter the sizes and elements of two arrays.
- Merges the two arrays into a single array.
- Displays the merged array.
Example:
- Input:
- Array 1:
[1, 3, 5]
- Array 2:
[2, 4, 6]
- Array 1:
- Output:
"Merged array: [1, 3, 5, 2, 4, 6]"
Solution Steps
- Read the Sizes and Elements of the Two Arrays: Use the
Scanner
class to take the sizes and elements of the two arrays as input from the user. - Create a Merged Array: Create a new array with a size equal to the sum of the sizes of the two arrays.
- Copy Elements from Both Arrays: Use
System.arraycopy
or a loop to copy elements from both arrays into the merged array. - Display the Merged Array: Print the merged array.
Java Program
// Java Program to Merge Two Arrays // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class MergeArrays { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Read the size and elements of the first array System.out.print("Enter the size of the first array: "); int size1 = scanner.nextInt(); int[] array1 = new int[size1]; System.out.println("Enter the elements of the first array:"); for (int i = 0; i < size1; i++) { array1[i] = scanner.nextInt(); } // Step 1: Read the size and elements of the second array System.out.print("Enter the size of the second array: "); int size2 = scanner.nextInt(); int[] array2 = new int[size2]; System.out.println("Enter the elements of the second array:"); for (int i = 0; i < size2; i++) { array2[i] = scanner.nextInt(); } // Step 2: Create a merged array int[] mergedArray = new int[size1 + size2]; // Step 3: Copy elements from both arrays into the merged array System.arraycopy(array1, 0, mergedArray, 0, size1); System.arraycopy(array2, 0, mergedArray, size1, size2); // Step 4: Display the merged array System.out.println("Merged array:"); for (int i = 0; i < mergedArray.length; i++) { System.out.print(mergedArray[i] + " "); } } }
Explanation
Step 1: Read the Sizes and Elements of the Two Arrays
- The
Scanner
class is used to read the sizes of the two arrays and their elements. ThenextInt()
method captures the sizes and each element of the arrays.
Step 2: Create a Merged Array
- A new array
mergedArray
is created with a size equal to the sum of the sizes of the two input arrays (size1 + size2
).
Step 3: Copy Elements from Both Arrays
- The
System.arraycopy()
method is used to copy elements from the first array (array1
) to the beginning of themergedArray
. - The elements of the second array (
array2
) are then copied starting from the indexsize1
in themergedArray
.
Step 4: Display the Merged Array
- The program prints the elements of the
mergedArray
using afor
loop.
Output Example
Example 1:
Enter the size of the first array: 3 Enter the elements of the first array: 1 3 5 Enter the size of the second array: 3 Enter the elements of the second array: 2 4 6 Merged array: 1 3 5 2 4 6
Example 2:
Enter the size of the first array: 4 Enter the elements of the first array: 10 20 30 40 Enter the size of the second array: 2 Enter the elements of the second array: 50 60 Merged array: 10 20 30 40 50 60
Example 3:
Enter the size of the first array: 2 Enter the elements of the first array: 7 9 Enter the size of the second array: 3 Enter the elements of the second array: 8 10 12 Merged array: 7 9 8 10 12
Conclusion
This Java program demonstrates how to merge two arrays into a single array. The program covers essential concepts such as array manipulation, the use of System.arraycopy()
, and basic input/output operations. This exercise is valuable for understanding how to handle multiple arrays and merge their contents efficiently in Java programming.