Call the Generic parameterized method in java


What does the error mesaage meant and how to call the generic parameterized method from another class: Following is the error message:

The method add(int, Integer) in the type BinaryMinHeap<Integer> is not applicable for the arguments (int, Vertex<Integer>)

calling snippet:

 for(Vertex<Integer> vertex : graph.getAllVertex()){
                minHeap.add(Integer.MAX_VALUE, vertex);

method being called is:

  public void add(int weight,T key) {
            Node node = new Node();
            node.weight = weight;
            node.key = key;
            int size = allNodes.size();
            int current = size - 1;
            int parentIndex = (current - 1) / 2;
            nodePosition.put(node.key, current);

            while (parentIndex >= 0) {
                Node parentNode = allNodes.get(parentIndex);
                Node currentNode = allNodes.get(current);
                if (parentNode.weight > currentNode.weight) {
                    current = parentIndex;
                    parentIndex = (parentIndex - 1) / 2;
                } else {

Show source
| generics   | java   | graph   2016-11-08 09:11 1 Answers

Answers to Call the Generic parameterized method in java ( 1 )

  1. 2016-11-08 09:11

    I'm not sure what you are trying to accomplish, but you cannot pass a Vertex<Integer> into something that only takes Integer. If you want to add every element of the Vertex, use nested loop. Otherwise you have to map Vexter<Integer> to Integer somehow.

    See method signature: BinaryMinHeap<Integer>.add(int, Integer)

    However, you are trying to call it with Vertex<Integer> vertex like that: add(int, Vertex<Integer>), which is not applicable (as stated in the error)

Leave a reply to - Call the Generic parameterized method in java

◀ Go back