forked from FatimaAlazazi/DataStructure2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericToMergeArrays.java
More file actions
45 lines (39 loc) · 1.28 KB
/
GenericToMergeArrays.java
File metadata and controls
45 lines (39 loc) · 1.28 KB
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
package Task4;
import java.util.Arrays;
/*
Q1/ How to make Signature of Print() method is the return of merge method ?
*/
public class GenericToMergeArrays {
static Integer [] intArr ={1,2,3,4,5,6};
static Integer [] intArr2 ={7,8,9,10,11,12};
public static <A> A[] Merge(A[] array_1 , A[] array_2)
{
int temp=0;
A[] New_Array = (A[]) new Object[array_1.length+array_2.length];
for (int i = 0; i <New_Array.length ; i++) {
if (i<array_1.length)
New_Array[i]=array_1[i];
else {
New_Array[i] = array_2[temp];
temp++;
}
}
//System.out.println(Arrays.toString(New_Array));
return New_Array;
}
/////////////////////////////////////////////////////////
public static <A> void Print(A[] array)
{
System.out.print("[ ");
for (int i = 0; i <array.length ; i++) {
if(i<array.length-1)
System.out.print(array[i]+" , ");
else
System.out.println(array[i]+" ]");
}
}
/////////////////////////////////////////////////////////
public static void main(String[] args) {
Print( Merge(intArr,intArr2));
}
}