Search code examples
javageneralization

Java generalization - constructor cannot be applied to given types


I am doing an tutorial task on implementing Graph using Adjacency List but got problem with the constructor.

In the given GraphTester.java I have:

//Constructor cannot be applied to given types
FriendShipGraph<String> graph = new AdjList<String>();

Then the FriendShipGraph.java provides an interface:

public interface FriendshipGraph<T extends Object> {
    public static final int disconnectedDist = -1;

    public abstract void addVertex(T vertLabel);
    public abstract void addVertex(T srcLabel, T tarLabel);
    //Other abstract methods
}

So I need to write a class to implement a LinkedList:

public class SinglyLinkedList implements LinkedListInterface {
    private Node head;
    private int length;

    public int getLength() {
        return length;
    }

    public SinglyLinkedList() {
        head = null;
        length = 0;
    }

    //Other methods to manage the linked list

    public class Node
    {
        private String value;
        private Node nextNode;

        public Node(String value) {
            this.value = value;
            nextNode = null;
        }

        //Other methods to manage node
    }
}

And I have to use an array of LinkedList to implement the Graph:

public class AdjList <T extends Object> implements FriendshipGraph<T> {
    SinglyLinkedList[] AdjList = null;

    //This is the constructor containing the error
    public AdjList(T vertices) {
        int qty = Integer.parseInt((String) vertices);
        AdjList = new SinglyLinkedList[qty];

    for (int i = 0; i < AdjList.length; i++)
        AdjList[i] = new SinglyLinkedList();
    }
}

However when I write my own test file I create AdjList object like this without error but this is not what the class requires:

AdjList<String> aList = new AdjList<String>("9");

So anyone please suggest me how to fix the constructor. Thank you so much!


Solution

  • FriendShipGraph<String> graph = new AdjList<String>();
    

    You do not have a zero-argument constructor in AdjJust. A default zero-argument constructor is not generated if you provide your own constructor(s), as you did with AdjList(T vertices).

    You need to provide a default constructor. Maybe something like the following might be sufficient depending on other code that isn't shown:

    public class AdjList <T extends Object> implements FriendshipGraph<T> {
    
        SinglyLinkedList[] AdjList = null;
    
        public AdjList() {
    
        }
    
        //This is the constructor containing the error
        public AdjList(T vertices) {
            int qty = Integer.parseInt((String) vertices);
            AdjList = new SinglyLinkedList[qty];
    
            for (int i = 0; i < AdjList.length; i++)
                AdjList[i] = new SinglyLinkedList();
        }
    }
    

    I'm not exactly sure why you're passing a string to represent a quantity but this should at least fix the compilation error you're asking about.