This is because of the following line:
data2 = data;
What you are essentially doing is creating a reference to data in data2. So when you print data2[0] it's actually referring to data[0] and printing that.
In order to copy an array as opposed to referencing, you need to clone all the elements it contains.
EDIT: Ok, guess Mattike beat me to it with a far superior post *cough* Show off *cough* lol j/k
One thing I would like to point out:
quote:
Originally posted by Mattike
The error is that when you assign an array variable to a new variable, it is dependent of this original array. That is, when you change the original one, the new one follows.
I think Mattike already knows this but just expressed it ambiguously: it does not actually create a copy and then apply any changes made to the original to the new array, it only creates a reference to the original array. That is, that data2[]
is essentially data[].