Why does changing one array alters the other?
Because "a" and "b" reference the same array. There aren't two of them; assigning the value of "a" to "b" assigns the reference to the array, not a copy of the array.
When you assign numbers, you're dealing with primitive types. Even on a Number instance there's no method to update the value.
You can see the same "they're pointing to the same object" behavior with Date instances:
var d1 = new Date(), d2 = d1;
d1.setMonth(11); d1.setDate(25);
alert(d2.toString()); // alerts Christmas day
Why does changing an Array in JavaScript affect copies of the array?
An array in JavaScript is also an object and variables only hold a reference to an object, not the object itself. Thus both variables have a reference to the same object.
Your comparison with the number example is not correct btw. You assign a new value to copyOfMyNumber
. If you assign a new value to copyOfMyArray
it will not change myArray
either.
You can create a copy of an array using slice
[docs]:
var copyOfMyArray = myArray.slice(0);
But note that this only returns a shallow copy, i.e. objects inside the array will not be cloned.
Why changes is one array data changing the value of another array in C#
Why the arr[1] value is changing when changing the value of arr1[1]?
This is your first question, but keep it now after answering the second.
and similarly why it is changing when
((int[])o)[1] = 1000;
This is your second question.
Answer is by seeing what is the initial value of o
variable.
Object o = arr;
The previous line sets o
to arr
, but it's not copying the elements from arr
to o
. They're reference types, so, now both o
and arr
refers to the same memory block. Therefore, any changes made to arr
will affect o
and vice-versa. Because they're sharing the same memory.
Now, let's get back to your first question:
It's really the same answer as the second one.
int[] arr1 = (int[])o;
The previous line sets arr1
to o
, but again, they just holds the same memory address.
To summarize:
- You created
arr
- You created
o
and made its reference the same asarr
, so they both share the same memory. - You created
arr1
and made its reference the same aso
which also same asarr
. so,arr
andarr1
ando
, all have the same reference.
Any change to any of them will affect the others.
Changing value in one array changes value in another array
changed = original;
This line is setting 'changed' to 'original' so they're the same array with the same pointers. You need to copy the array instead of setting changed equal to original.
You can try using System.arraycopy()
Javascript copy array. Changes affect both arrays
When you copy via .slice
or [...]
it performs a shallow copy, which means the outer array is copied but the inner arrays aren't. You can individually copy each element:
let ans = board.map(v => v.slice()); // copies every 1d array
Why is one of these functions changing the original array and not the other?
In the last example you have array
coming in as a local variable. Reassigning the local variable has zero effect on the original variable. This is because Ruby is, in effect, pass by value, except the confusing part is those values are often object references, or a fancy term for pointers.
The consequences of this aren't that complicated though: Calling a method on an object can modify the contents of that object. Reassigning a variable cannot.
The first version of your method is probably the best, but you could make it more generic, like:
def multiply_by!(array, n = 2)
array.map! { |v| v * n }
end
You can also "fix" the last version by using a method call to apply the changes:
def array_times_two!(array)
array.replace(array.map {|x| x * 2})
end
This calls Array#replace
to stomp the contents of the original object and force the changes to stick. It's not a very elegant solution when things like map!
exist.
Why making changes to copy of array , affects to original array?
The spread operator creates a shallow copy. If the content of a menu was objects, then changing those objects in a copy array will change those objects in the original array (or technically, those two references are for the same object):
const obj1 = {
val: 1
}
const obj2 = {
val: 2
}
const obj3 = {
val: 3
}
const arr = [obj1, obj2, obj3]
// creating a copy with the spread operator
const copy = [...arr]
// changing the second element in the copy
copy[1].val = 22
// the element in the original array is changed too
console.log(arr)
Related Topics
Jquery Get Values of Checked Checkboxes into Array
Keycode Values for Numeric Keypad
How to Make 'Setinterval' Behave More in Sync, or How to Use 'Settimeout' Instead
How to Get the Last Character of a String
How Dangerous Is It in JavaScript, Really, to Assume Undefined Is Not Overwritten
Understanding React-Redux and Mapstatetoprops()
How to Pass Arguments to Event Handlers in Jquery
How to Access Object Property with Invalid Characters
Array.Prototype.Includes VS. Array.Prototype.Indexof
How to Create a Hash or Dictionary Object in JavaScript
ASP.NET MVC 3 Razor: Include JavaScript File in the Head Tag
Find All Text Nodes in HTML Page
Can't Access Cookies from Document.Cookie in Js, But Browser Shows Cookies Exist