I'm instantiating one array 'array1' by another 'array2' and then attempting to modify 'array2', which changes 'array1'. After many hours I realised that it may be a call-by- or a pass-by- ref/value error. Can someone please help me solve this and put me in the right direction?
int[] src = {0,4,3,2,1};
int[] dest = src;
dest[0] = dest[0] + 2;
for (int node: dest) {
System.out.print(node + " ");
}
System.out.println("");
for (int node: src) {
System.out.print(node + " ");
}
This produces:
2 4 3 2 1
2 4 3 2 1
i.e. the source array gets modified too. Thanks in advance.
srcanddest) hold the same reference, so any change in the state on one of them will be reflected in the other.destwithout modifyingsrc?System.arrayCopyand save some time.