forked from FatimaAlazazi/DataStructure2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericToMergeArrays2.java
More file actions
35 lines (27 loc) · 965 Bytes
/
GenericToMergeArrays2.java
File metadata and controls
35 lines (27 loc) · 965 Bytes
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
package Task4;
import java.lang.reflect.Array;
public class GenericToMergeArrays2 {
static Integer [] intArr_1 ={1,2,3,4,5,6};
static Integer [] intArr_2 ={7,8,9,10,11};
public static<T> T[] concatenate(T[] first, T[] second)
{
T[] ob = (T[]) Array.newInstance(first.getClass().getComponentType(),
first.length + second.length);
System.arraycopy(first, 0, ob, 0, first.length);
System.arraycopy(second, 0, ob, first.length, second.length);
return ob;
}
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(concatenate(intArr_1,intArr_2));
}
}