0

When I ask for the length in the test I get an error with the casting of the integer, I tried with other classes like Character instead of Integer but I get the same error, java does not let me write Object or Comparable as parametrized classes.

That's a JUnit Test Case

public class EDBinaryHeapTest {
    @Test
    public void constructorTest01() {
        EDBinaryHeap<Integer> toCheck = new EDBinaryHeap<Integer>(0);
        
        assertEquals(0, toCheck.numElementos);
        assertEquals(0, toCheck.elementos.length); 
    }
}

That's the class I`m creating to check

 public class EDBinaryHeap<T extends Comparable<T>> implements EDPriorityQueue<T>{
    protected T[] elementos;
    protected int numElementos;

    @SuppressWarnings("unchecked")
    public EDBinaryHeap(int numMaxElementos) {
        elementos = (T[]) new Comparable[numMaxElementos];
    }
}

1 Answers1

0

Few problems here:

  1. You can't initiate Comparable.
  2. Your T class extends Comparable and not vice versa.

To solve this you can use:

    elementos = (T[])Array.newInstance(type, size);