Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deep cloning not working

 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why are you using Integers rather than ints in an array called intArray?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic