PDA

View Full Version : [SOLVED] Copying arrays in Java



dwally89
November 11th, 2009, 10:27 AM
Hi,

I have a two dimension array that stores objects.
How do I copy these objects over to a new array?

I want to copy the objects themselves, and not the references to the objects.

e.g. Say I have an array cardLocation[13][25], that is of type Card, and I want to copy all the objects of it over to an array tempArray[13][25], how would I do this?

Thanks

u2ix
November 11th, 2009, 12:07 PM
you may use the clone() method.


tempArray[13][25] = cardLocation[13][25].clone();

so you create a equal object

dwally89
November 11th, 2009, 12:12 PM
Thanks.
I actually already knew about the clone method, but I just realised I didn't apply it in all the needed places.

Thanks again

Ruhe
November 11th, 2009, 01:12 PM
Use System.arraycopy, Luke ;)

dwally89
November 11th, 2009, 02:16 PM
What's the difference between System.arraycopy and .clone()?

(.clone() seemed to work for me)

Ruhe
November 11th, 2009, 03:20 PM
What's the difference between System.arraycopy and .clone()?

(.clone() seemed to work for me)

Interesting question.
I thought that array.clone() returns deep copy of array,
so if you clone array, which consists of cloneable elements, then all the elements will be cloned.
But it seems, this is not true. Both methods just copy elements from one array to another.

Anyway it's useful to know about such method.

By the way, System.arraycopy is used a lot in java.util.ArrayList

dwally89
November 11th, 2009, 03:22 PM
So what's the difference then?

0cton
November 11th, 2009, 03:39 PM
clone() is meant to make a copy of an object , note that you must do your own clone method for your own objects.
arraycopy() is a method that takes to arrays and copies the values from one to another, I imagine it is doing it for each index i am not sure :P

dwally89
November 11th, 2009, 04:08 PM
Hmm...

I used clone and it worked, even though I didn't make my own clone method...

Ruhe
November 11th, 2009, 05:52 PM
Checkout this (http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.7) section of java language specification. Look at the example with cloning array of integers.

This is what I was talking about.

PS: but it doesn't clone array of my own cloneable objects. I can't understand this.