• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deep copy of PriorityQueue class  RSS feed

 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm working through an implementation of the A* algorithm, and I want to make a deep copy, not just a reference of a PriorityQueue object called 'openList' and assign it to another PriorityQueue object called 'tempList'. When I try to do openList = tempList.clone(), it doesn't work. Suggestions or ideas on how to do this easily and simply? I'm pretty far into my project and would rather not have to implement a new Priority Queue class just to do this. Thanks!
 
Anshuman Chakraborty
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,

PriorityQueue doesn't override clone() method.
The only option left is Iterate through the queue, clone the elements and assign it to a new queue.

Now cloning can be done using either of the two steps

1. Override the clone method.
2. Else create a new object using existing object.

Correct me if I am wrong.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim --

Do you want to copy just the PriorityQueue, or the objects it contains? If it's the latter, I believe Anshuman Chakraborty is correct. But in the more likely scenario that it's the former, then there's a PriorityQueue constructor for this: just construct a new PriorityQueue with the old one as the sole constructor argument, and you'll get a copy you can manipulate without messing up the original.
 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Jim --

Do you want to copy just the PriorityQueue, or the objects it contains? If it's the latter, I believe Anshuman Chakraborty is correct. But in the more likely scenario that it's the former, then there's a PriorityQueue constructor for this: just construct a new PriorityQueue with the old one as the sole constructor argument, and you'll get a copy you can manipulate without messing up the original.


Awesome, that worked. thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!