
Signature
[ 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
>> When I attempt to overwrite or copy one array to another, using
>> something like :
[quoted text clipped - 22 lines]
>
> 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
Gordon Beaton - 14 Jun 2005 12:46 GMT
> i've tried that, but it still seems to 'associate' the two arrays. For
> example I used:
[quoted text clipped - 3 lines]
> 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

Signature
[ 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
Alexander Lueck - 14 Jun 2005 12:53 GMT
> 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?
If you create a new array Lmatrix (i.e. int[] Lmatrix = new int[..]) the
two arrays "aren't linked".
Have a look at the following:
int[] array1 = {5, 4, 3, 2, 1, 0};
int[] array2 = new int[array1.length];
System.arraycopy(array1, 0, array2, 0, array1.length);
for (int i=0; i<array1.length; i++) array1[i] = i;
for (int i=0; i<array1.length; i++) System.out.print(array1[i]+" ");
System.out.println();
for (int i=0; i<array2.length; i++) System.out.print(array2[i]+" ");
System.out.println();
The output is:
0 1 2 3 4 5
5 4 3 2 1 0
Now have a look at this (which only differs in the second line):
int[] array1 = {5, 4, 3, 2, 1, 0};
int[] array2 = array1;
System.arraycopy(array1, 0, array2, 0, array1.length);
for (int i=0; i<array1.length; i++) array1[i] = i;
for (int i=0; i<array1.length; i++) System.out.print(array1[i]+" ");
System.out.println();
for (int i=0; i<array2.length; i++) System.out.print(array2[i]+" ");
System.out.println();
The output is:
0 1 2 3 4 5
0 1 2 3 4 5
The reason is that in the first example you have two arrays, the first
referenced by array1 and the second referenced by array2. In the second
example you have just one array because array2 references the same array
as array1. (There is no second array created but only the reference to
the first array is copied.)
One more comment on the line
System.arraycopy(Pmatrix, 1, Lmatrix, 1, size)
Notice that the first element of an array has the index 0 not 1.
Greetings
Alex
Thomas G. Marshall - 15 Jun 2005 01:31 GMT
Alexander Lueck coughed up:
>> i've tried that, but it still seems to 'associate' the two arrays. For
>> example I used:
[quoted text clipped - 9 lines]
>
> int[] array1 = {5, 4, 3, 2, 1, 0};
This isn't going to help him if what he's got is an array of objects (object
references). I'm fairly sure that that is what's confusing him. He's
probably building two distinct arrays, but each holding the same references
as the other.
This would make it /look/ as if the arrays were linked.
...[rip]...

Signature
Whyowhydidn'tsunmakejavarequireanuppercaselettertostartclassnames....
Boudewijn Dijkstra - 14 Jun 2005 22:02 GMT
>>> When I attempt to overwrite or copy one array to another, using
>>> something like :
[quoted text clipped - 29 lines]
> which copies Pmatrix to Lmatrix, but still seems to have the effect of
> linking changes between the two. anything im missing here?
If the element type of the arrays is (also) a reference type, then copying the
arrays and then fiddling with the contents, would have about the same effect
as your first attempt. When copying objects that contain objects (that
contain objects), a so-called "deep copy" will recursively copy every bit of
information. The java.util.Arrays class provides methods for doing this.