-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeppCpyArr.java
More file actions
34 lines (28 loc) · 984 Bytes
/
Copy pathdeppCpyArr.java
File metadata and controls
34 lines (28 loc) · 984 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
public class deppCpyArr
{
public static void printArr(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args) {
int[] arr={1,2,3,4,5}; //create memory in heap and stack
int[] arr_2=arr.clone(); //seprate memory is created for arr_2 in heap
//Deep copy of Array
System.out.println("Original Array");
printArr(arr);
System.out.println("\ncopied Array");
printArr(arr_2);
//changes in arr_2
arr_2[0]=0;
arr_2[1]=0;
System.out.println("\narr_2 after changes");
printArr(arr_2);
System.out.println("\narr after changes in arr_2");
printArr(arr);
//Now both having their own memory in heap that's why any changes in perticular array can't affect
//on each other
}
}