This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Deep cloning not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Deep cloning not working" Watch "Deep cloning not working" New topic

Deep cloning not working

nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 83
I was doing some exercise as part of self learning to implement different sort algorithm and as an additional test, wanted to experiment with deep cloning as well. however the deep cloning is not working in the intended way. may be I am missing something obvious but am not able to get it and hence need some advice. here is the code

and here is the output

before sorting the array is -
after sorting the array is -
copied array

Matthew Brown

Joined: Apr 06, 2010
Posts: 4344

What were you expecting the result to be? You sort the array, then you take a copy. So the copy is going to match the sorted array. Are you sure you're doing things in the order you intended?
Rob Spoor

Joined: Oct 27, 2005
Posts: 19656

Also, your clone() method isn't written quite well. You shouldn't use new to create clones, or sub classes cannot override clone() well. I suggest you change it to this, also keeping the CloneNotSupportedException local to the clone() method:

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38075
And why are you using Integers rather than ints in an array called intArray?
I agree. Here's the link:
subject: Deep cloning not working
Similar Threads
Large arrays
Generic Interface Example
Array Problem
Doubt in ArrayList's toArray function