File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


JavaRanch » Java Forums » Java » Java in General
Bookmark "Deep cloning not working" Watch "Deep cloning not working" New topic
Author

Deep cloning not working

nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 83
Hi,
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 -
10,15,5,91,14,
after sorting the array is -
5,10,14,15,91,
copied array
5,10,14,15,91,

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
And why are you using Integers rather than ints in an array called intArray?
 
GeeCON Prague 2014
 
subject: Deep cloning not working