• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deep copy of PriorityQueue class

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic