forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeTwoArrays.java
52 lines (45 loc) · 1.23 KB
/
mergeTwoArrays.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
43
44
45
46
47
48
49
50
51
52
package com.techvidvan.mergearrays;
import java.util.Arrays;
public class ManualMethod {
public static void main(String[] args) {
//declaring two character arrays
int[] array1 = {
1,
2,
3,
4
};
int[] array2 = {
5,
6,
7,
8
};
//Calculating sum of length of two arrays
int length = array1.length + array2.length;
System.out.println("First Array is: ");
for (int i = 0; i < array1.length; i++) {
System.out.print(" " + array1[i]);
}
System.out.println(" ");
System.out.println("Second Array is: ");
for (int i = 0; i < array2.length; i++) {
System.out.print(" " + array2[i]);
}
//Creating a resulting array of the calculated length
int[] result = new int[length];
int position = 0;
//for each loop to add array1 elements to the resulting array
for (int element: array1) {
result[position] = element;
position++;
}
//for each loop to add array2 elements to the resulting array
for (int element: array2) {
result[position] = element;
position++;
}
System.out.println("\nThe resulting array after merging two arrays is: ");
System.out.println(Arrays.toString(result));
}
}