Merge Two Arrays
Write a program to create two arrays of size m and n, merge them into a third array and display the merged array.
import java.io.*;
class Array_Merge
{
public static void main ( String agrs[])throws IOException
{
BufferedReader obj= new BufferedReader(
new InputStreamReader(System.in));
int i,j,m,n,k;
boolean flag=false;
System.out.println("Enter the length of Ist array : ");
m=Integer.parseInt(obj.readLine());
int a[]=new int[m];
System.out.println("Enter "+m+" elements for the Ist array");
for(i=0;i< m;i++)
{
a[i]=Integer.parseInt(obj.readLine());
}
System.out.println("Enter the length of IInd array : ");
n=Integer.parseInt(obj.readLine());
int b[]=new int[n];
System.out.println("Enter "+n+" elements for the IInd array");
for(i=0;i< n;i++)
{
b[i]=Integer.parseInt(obj.readLine());
}
int c[]=new int[(m+n)];
k=0;
for(i=0;i< m;i++)
c[k++]=a[i];
for(i=0;i< n;i++)
c[k++]=b[i];
System.out.println("Merged array");
for(i=0;i< m+n;i++)
System.out.print(c[i]+" ");
}
}
Have something to say? Log in to comment on this post.
0 comments