Java Program to merge two Arrays
Get two arrays and merge them into single array.
Sample Input 1:
3
5 4 2
4
9 7 6 3
Sample Output 1:
5 4 2 9 7 6 3
Try your Solution
Strongly recommended to Solve it on your own, Don't directly go to the solution given below.
public class Hello
{
public static void main(String args[])
{
//Write your code here
}
}
Program or Solution
//Merging Two Arrays...
import java.util.*;
class Program
{
public static void main(String args[])
{
int i,j=0,sz1,sz2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Size Of First Array:");
sz1=sc.nextInt();
int a[]=new int[sz1];
System.out.println("Enter The First Array:");
for(i=0;i<sz1;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter The Size Of Second Array:");
sz2=sc.nextInt();
int b[]=new int[sz2];
int c[]=new int[sz1+sz2];
System.out.println("Enter The Second Array:");
for(i=0;i<sz2;i++)
{
b[i]=sc.nextInt();
}
for(i=0;i<sz1;i++)
{
c[j]=a[i];
j++;
}
for(i=0;i<sz2;i++)
{
c[j]=b[i];
j++;
}
System.out.println("The Merging Of Two Arrays:");
for(i=0;i<sz1+sz2;i++)
{
System.out.println(" "+c[i]);
}
}
}
Program Explanation
Comments
Related Programs
- Java Program to get and print the array elements
- Java Program to find the sum of array elements.
- Java Program to find the average of array Elements
- Java Program to Search an Element in an array
- Java Program to find Largest element in the array
- Java Program to find Smallest element in the array
- Java Program to print all the numbers which are less than given key element from a given array.
- Java Program to Sort the Elements in ascending order.
- Java Program to Sort the Elements in descending order.
- Addition of Matrix in Java
- Java Program to delete an Element from the Array
- Java Program to Multiply two Matrices
- Java Program to Reverse the Elements of Array
- Java Program to Reverse the First Half Elements of Array
- Java Program to Reverse the Second Half Elements of Array
- Java Program to find the Second Largest Number in the Array
- Java Program to find the Second Smallest Number in the Array
- Java Program to transpose the Matrix
coming Soon
coming Soon