1

I found very similar questions to mine here, but I cannot work my problem out with the other posts. Sorry for the double posting maybe...

I am trying to run my code in my terminal but I am getting a javac error, even though my code seems to be fine in Eclipse.

      public class MinHeap<E extends Comparable<E>> {
         List<E> h = new ArrayList<E>();
         ArrayList<E> arrayPostingsList = new ArrayList<E>();
     //some more code

        public double remove() {
          E removedNode = h.get(0);
          E lastNode = h.remove(h.size() - 1);
          percolateDown(0, lastNode);
           //this seems to be the problem
          return (Double) removedNode;
      }

This is the error I get

   MinHeap.java:40: inconvertible types
   found   : E
   required: java.lang.Double
   Double B = (Double) removedNode;
                    ^
   1 error

Any tips?

With all your input I have changed the return type and it is working fine.

         public E remove() {
           E removedNode = h.get(0);
           E lastNode = h.remove(h.size() - 1);
           percolateDown(0, lastNode);

         return removedNode;
        }

Thanks!

Bipa
  • 189
  • 2
  • 3
  • 14
  • `Double` is a wrapper class, while `double` is a primitive type. They're not the same, and the compiler will get mad if you try to return an object when it needs a primitive. – Luiggi Mendoza Oct 12 '12 at 04:26

3 Answers3

3

Since the element that is held by the MinHeap is of type E, you are removing an element of the same type and so the return type of remove() method should also be E.

When you declare an instance of MinHeap holding Double type objects, then the return type would become Double anyway and you can write it as follows:

MinHeap<Double> minHeap = ....
....
Double removedValue = minHeap.remove();

Your code is giving compile error because it doesn't make sense for a MinHeap holding String objects, right?

Vikdor
  • 23,934
  • 10
  • 61
  • 84
0

This should do

public class MinHeap<E extends Comparable<E>> {
    List<E> h = new ArrayList<E>();
    ArrayList<E> arrayPostingsList = new ArrayList<E>();

    // some more code
    public static void main(String[] args) {
        MinHeap<Double> testHeap = new MinHeap<Double>();
        testHeap.add(2.3); // auto-boxing
        System.out.println(testHeap.remove()); //unboxing

    }

    public boolean add(E e) {
        return h.add(e);
    }

    public E remove() {
        E removedNode = h.get(0);
        E lastNode = h.remove(h.size() - 1);

        // this seems to be the problem
        return removedNode;
    }
}
sakthisundar
  • 3,278
  • 3
  • 16
  • 29
0

I think your eclipse is using different java version than one available in command line. I guess you are using Java 1.7.x in eclipse and some lower version(e.g. 1.6.x) in command line. Please set the same version of java in command line and check. It should behave the same.

Try checking java version on command line by using java -version. I am positive, its JDK 1.6 or below as above code compiles fine with JDK 1.7 through command line as well as eclipse but gives the mentioned error with JDK 1.6.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73