Current heap getting updated in java
<p>I am writing a class for a heap in java.</p>

<p>The class heap has</p>

<pre><code> class Heap{
     int maxsize=1000;
     int[] heap= new int[maxsize];
     int size=0;
     //.... some methods basically, print, insert and remove

     int[] sortHeap (){
         int[] sorted= new int[size];
         Heap copy= new Heap();
         copy.heap=heap;
         copy.size=size;
         int i=0;
         while (copy.size&gt;0){
             sorted[i]=copy.remove();
             i++;
         }
         return sorted;
     }
 }
</code></pre>

<p>One method I am trying to create is returning the sorted heap. I do not want to destroy the original heap.
However when I call this method, the original heap from which I am calling it gets destroyed.
Can some one explain to me why does this happen?</p>

<p>eg.
Say heap is
-17,<br>
-1,  -7,<br>
1,  0,  2,  -5,<br>
17,  57,  27,  3,  127,  9,  // correctly printed</p>

<p>Now I call test.heapSort(); and then print the resultant array.</p>

<p>Then printing the heap after this gives the result</p>

<p>127,<br>
127,  127,<br>
57,  57,  127,  27,<br>
27,  57,  27,  3,  127,  9,</p>

<p>Thank you.</p>
