File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Shallow  copy of array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Shallow  copy of array" Watch "Shallow  copy of array" New topic

Shallow copy of array

Denis Yuzvyk

Joined: Dec 06, 2004
Posts: 22
I have array of my classes:

MyClass [] arr = new MyClass [] {new MyClass(), new MyClass ()};

how to make shalow copy of arr?
arr.clone() will be enough?
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
ya doing
MyClass[] arrClone = (MyClass[])arr.clone();

will be enough.

thanks,<br />gaurav abbi
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
All arrays in Java, irrespective of their types, inter se implement & java.lang.Cloneable. Thus calling clone() on a reference to an array object will not throw a CloneNotSupportedException, albeit, you will have to catch the exception (as it is a checked exception).

SCJP 5.0 (100%)
I agree. Here's the link:
subject: Shallow copy of array
It's not a secret anymore!