In Java for example:
int x[] = {1,2,3,4};
int y[] = x;
y[0] = 100;
System.out.println(x[0]);
System.out.println(y[0]);
Output:
100
100
How can I change the values of int y
without changing values of int x
?
In Java for example:
int x[] = {1,2,3,4};
int y[] = x;
y[0] = 100;
System.out.println(x[0]);
System.out.println(y[0]);
Output:
100
100
How can I change the values of int y
without changing values of int x
?
You are pointing to same array location hence updating one will update the second.
Use clone() method to create copy of the array.
int x[] = {1,2,3,4};
int y[] = x.clone();
y[0] = 100;
System.out.println(x[0]);
System.out.println(y[0]);
Output :
1
100