Java generalization - constructor cannot be applied to given types

蹲街弑〆低调 提交于 2019-12-12 03:48:44

问题


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!


回答1:


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.




回答2:


In addition to the correct answer from Trey, some more remarks:

Your one-arg constructor says T vertices; but then you are doing a "hard" cast to (String) in there. That code will throw an exception if T is anything else but String.

So, you should either make AdjList (horrible name by the way) go like class AdjList implements FriendshipGraph<String>; or when you don't want to "fix" the generic type to string, you could go for qty = Integer.parseInt(verties.toString())

But looking at that - doesn't that sound weird? You know, turning something that seems to be a number into a string, to parse a number from it? Maybe it should be an Integer all the time?

Then: work on your naming. There is absolutely no need to use abbreviations like "qty"; why don't you call it numberOfLists or something alike?!



来源:https://stackoverflow.com/questions/39240079/java-generalization-constructor-cannot-be-applied-to-given-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!