Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

overwriting arrays

Thread view: 
Jeremy Watts - 14 Jun 2005 10:10 GMT
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
Gordon Beaton - 14 Jun 2005 10:28 GMT
> When I attempt to overwrite or copy one array to another, using
> something like :
[quoted text clipped - 4 lines]
> 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

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

Jeremy Watts - 14 Jun 2005 12:11 GMT
>> 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.
TechBookReport - 14 Jun 2005 10:28 GMT
> Hi,
>
[quoted text clipped - 15 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.