-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeArrays.java
42 lines (42 loc) · 1.06 KB
/
mergeArrays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.*;
public class mergeArrays
{
public static void main(String[] args)
{
// first array
int a[] = { 30, 25, 40 };
// second array
int b[] = { 45, 50, 55, 60, 65 };
// determining length of first array
int a1 = a.length;
// determining length of second array
int b1 = b.length;
// resultant array size
int c1 = a1 + b1;
// Creating a new array
int[] c = new int[c1];
// Loop to store the elements of first
// array into resultant array
for (int i = 0; i < a1; i = i + 1)
{
// Storing the elements in
// the resultant array
c[i] = a[i];
}
// Loop to concat the elements of second
// array into resultant array
for (int i = 0; i < b1; i = i + 1)
{
// Storing the elements in the
// resultant array
c[a1 + i] = b[i];
}
// Loop to print the elements of
// resultant array after merging
for (int i = 0; i < c1; i = i + 1)
{
// print the element
System.out.println(c[i]);
}
}
}