View Full Version : [SOLVED] Copying arrays in Java

November 11th, 2009, 10:27 AM

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?


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

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

Thanks again

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

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

(.clone() seemed to work for me)

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

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

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

November 11th, 2009, 04:08 PM

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

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.