|
From: Jeremy Watts on 14 Jun 2005 05:10 Hi, When I attempt to overwrite or copy one array to another, using something like : array1 = array2; then the two arrays seem to become 'linked' or associated with eachother, so that if I made a change to array2 then that same change is also performed on array1. Is this unique to Java?? I have programmed in PHP in the past and it has not seemed to have done this. Is there a way to turn this feature off or to get around it somehow.? What I am after is a way of simply copying one array to another and to have those two separate arrays to behave independently of eachother. Thanks Jeremy
From: Gordon Beaton on 14 Jun 2005 05:28 On Tue, 14 Jun 2005 09:10:58 GMT, Jeremy Watts wrote: > When I attempt to overwrite or copy one array to another, using > something like : > > array1 = array2; > > then the two arrays seem to become 'linked' or associated with > eachother, so that if I made a change to array2 then that same > change is also performed on array1. array1 and array2 are array *references* (think: "pointers"). The assignment does not change the contents of either array. After the operation, array1 and array2 both refer to the *same* array, the one previously referred to by array2. > Is this unique to Java?? Not at all. If you had done the equivalent operation in C using pointers, you would have experienced the same thing. > I have programmed in PHP in the past and it has not seemed to have > done this. Is there a way to turn this feature off or to get around > it somehow.? System.arrayCopy(); /gordon -- [ do not email me copies of your followups ] g o r d o n + n e w s @ b a l d e r 1 3 . s e
From: TechBookReport on 14 Jun 2005 05:28 Jeremy Watts wrote: > Hi, > > When I attempt to overwrite or copy one array to another, using something > like : > > array1 = array2; > > then the two arrays seem to become 'linked' or associated with eachother, so > that if I made a change to array2 then that same change is also performed on > array1. Is this unique to Java?? > > I have programmed in PHP in the past and it has not seemed to have done > this. Is there a way to turn this feature off or to get around it somehow.? > > What I am after is a way of simply copying one array to another and to have > those two separate arrays to behave independently of eachother. > > Thanks > Jeremy > > In Java every array is an object. This means that array1 and array2 are references to objects (like pointers). By setting array1=array2 you are pointing array1 to the same object as array2. If you want to make a copy of the array take a look at how array cloning. Pan ============================================================================ TechBookReport Java http://www.techbookreport.com/JavaIndex.html
From: Jeremy Watts on 14 Jun 2005 07:11 "Gordon Beaton" <not(a)for.email> wrote in message news:42aea341$1(a)news.wineasy.se... > On Tue, 14 Jun 2005 09:10:58 GMT, Jeremy Watts wrote: >> When I attempt to overwrite or copy one array to another, using >> something like : >> >> array1 = array2; >> >> then the two arrays seem to become 'linked' or associated with >> eachother, so that if I made a change to array2 then that same >> change is also performed on array1. > > array1 and array2 are array *references* (think: "pointers"). The > assignment does not change the contents of either array. After the > operation, array1 and array2 both refer to the *same* array, the one > previously referred to by array2. > >> Is this unique to Java?? > > Not at all. > > If you had done the equivalent operation in C using > pointers, you would have experienced the same thing. > >> I have programmed in PHP in the past and it has not seemed to have >> done this. Is there a way to turn this feature off or to get around >> it somehow.? > > System.arrayCopy(); i've tried that, but it still seems to 'associate' the two arrays. For example I used: System.arraycopy(Pmatrix, 1, Lmatrix, 1, size); which copies Pmatrix to Lmatrix, but still seems to have the effect of linking changes between the two. anything im missing here? > > /gordon > > -- > [ do not email me copies of your followups ] > g o r d o n + n e w s @ b a l d e r 1 3 . s e
From: Gordon Beaton on 14 Jun 2005 07:46
On Tue, 14 Jun 2005 11:11:16 GMT, Jeremy Watts wrote: > i've tried that, but it still seems to 'associate' the two arrays. For > example I used: > > System.arraycopy(Pmatrix, 1, Lmatrix, 1, size); > > which copies Pmatrix to Lmatrix, but still seems to have the effect of > linking changes between the two. anything im missing here? I'll assume that you've in fact created two separate arrays to begin with, and that it was your intention to copy from the *second* element (at position 1). What I wrote earlier about array references is true of references to *any* object. The above call to System.arraycopy() does basically this: for (int i=1; i<size; i++) { Lmatrix[i] = Pmatrix[i]; } If your arrays contain object references, then each array holds its own copy of the references afterwards and can be manipulated independently of the other, but the referred objects themselves will be shared. In that case, you need to make copies of the objects (not copies of the object references) when you copy the array elements. Exactly how you do that depends on what the objects are (and needs their support), but look into using clone() or a copy constructor. It might look something like this: for (int i=1; i<size; i++) { Lmatrix[i] = Pmatrix[i].clone(); } or this: for (int i=1; i<size; i++) { Lmatrix[i] = new Gurka(Pmatrix[i]); } /gordon -- [ do not email me copies of your followups ] g o r d o n + n e w s @ b a l d e r 1 3 . s e |