title: Implementing MaxHeap with Key,Value Pair
body: <p>I'm implementing a MaxHeap in Java that is sorted on the basis of its key. Each key is also associated with a value. When I try to run my program I get an Exception : <code>Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LMaxHeap$Obj;</code></p>
  
  <p><strong>EDIT</strong> The exception is thrown on this line : <code>o = (Obj[])new Object[n+1];</code></p>
  
  <p>How do I go about solving my problem ? </p>
  
  <p>Here is my code: </p>
  
  <pre><code>public class MaxHeap&lt;Key,Value&gt; {
  
      Obj [] o;
      int N;
      int size;
      private Comparator&lt;Key&gt; comparator;
  
      public MaxHeap(int n){
          o = (Obj[])new Object[n+1];
          size =0;
          N = n;
      }
  
      public class Obj{
          Key k;
          Value v;
  
          public Obj(Key k, Value v){
              this.k = k;
              this.v = v;
          }
      }
  
  
  void push(Key k, Value v) {
          Obj temp = new Obj(k,v);
          o[++size] = temp;
          swim(size);
      }
  
  Obj pop() {
      Obj del = o[1];
  
      Obj temp = o[1];
      o[1] = o[size];
      o[size--] = temp;
  
      sink(1);
      return del;
  }
  
  boolean isLess(int i, int j){
      return ((Comparable&lt;Key&gt;) o[i].k).compareTo(o[j].k) &lt; 0;
      }
  
  void swim(int index){
      Obj temp;
      while(index &gt; 1 &amp;&amp; isLess(index/2,index)){
              temp = o[index];
              o[index] = o[index/2];
              o[index/2] = temp;
  
              index = index/2;
          }
      }
  
  void sink(int index){
      int i;
      Obj temp;
      while(2*index &lt;= size){
          i = 2*index;
          if(i &lt; size &amp;&amp; isLess(i, i+1))
              i++;
          if(!isLess(index,i))
              break;  
          temp = o[index];
          o[index] = o[i];
          o[i] = temp;
  
          index = i;
          }
      }
  
  }
  </code></pre>