Page 353 - ComputerScience_Class_11
P. 353
Here two array elements are merged and a new array is created. Suppose the two arrays are ar1 and ar2.
Index 0 1 2 3 4
Ar1 10 5 14 7 66
Index 0 1 2
Ar2 11 22 33
The merged array ar3 is:
Index 0 1 2 3 4 2 3 4
Ar 10 5 14 7 66 11 22 33
Note: The third array created will have the size of the total size of 1 and 2 arrays.
st
nd
Program 16 Write a Java program to merge two array elements and display the new array.
1 import java.util.*;
2 class merge
3 {
4 public static void main(String args[])
5 {
6 Scanner sc= new Scanner(System.in);
7 int ar1[ ]=new int[5];
8 int ar2[ ]=new int[5];
9 int ar3[ ]=new int[10];
10 int i, j;
11 for (i=0; i<5; i++)
12 {
13 System.out.print("Enter element in 1st array: ");
14 ar1[i] =sc.nextInt();
15 }
16 for (i=0; i<5; i++)
17 {
18 System.out.print("Enter element in 2nd array: ");
19 ar2[i] =sc.nextInt();
20 }
21 //Merging the two arrays
Arrays 351

