Java泛化-构造函数不能应用于给定类型

Java泛化-构造函数不能应用于给定类型,java,generalization,Java,Generalization,我正在做一个关于使用邻接列表实现图形的教程任务,但遇到了构造函数的问题 在给定的GraphTester.java中,我有: //Constructor cannot be applied to given types FriendShipGraph<String> graph = new AdjList<String>(); 因此,我需要编写一个类来实现LinkedList: public class SinglyLinkedList implements Linked

我正在做一个关于使用邻接列表实现图形的教程任务,但遇到了构造函数的问题

在给定的
GraphTester.java
中,我有:

//Constructor cannot be applied to given types
FriendShipGraph<String> graph = new AdjList<String>();
因此,我需要编写一个类来实现
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
    }
}
我必须使用
LinkedList
数组来实现
图形

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();
    }
}
public类AdjList实现友谊图{
SingleLinkedList[]AdjList=null;
//这是包含错误的构造函数
公共调整列表(T顶点){
int数量=整数.parseInt((字符串)顶点);
AdjList=新的单链接列表[数量];
for(int i=0;i
然而,当我编写自己的测试文件时,我创建了如下的AdjList对象,没有错误,但这不是类所要求的:

AdjList<String> aList = new AdjList<String>("9");
AdjList aList=新的AdjList(“9”);
因此,任何人请建议我如何修复构造函数。非常感谢你

FriendShipGraph<String> graph = new AdjList<String>();
我不太清楚为什么要传递一个字符串来表示一个数量,但这至少可以修复您所问的编译错误


我不太清楚为什么要传递一个字符串来表示一个数量,但这至少可以修复您所问的编译错误。

除了Trey给出的正确答案之外,还有一些备注:

您的单参数构造函数表示
T顶点
;但是你在那里做了一个“硬”演员。如果T不是字符串,则该代码将抛出异常

因此,您应该要么让AdjList(顺便说一句,这个可怕的名字)像
类AdjList实现了友谊图
;或者,当您不想“修复”字符串的泛型类型时,可以选择
qty=Integer.parseInt(verties.toString())

但是看看这个-听起来不是很奇怪吗?你知道,把一个看起来像数字的东西变成一个字符串,从中解析一个数字?也许它应该一直是一个整数


然后:进行命名。绝对不需要使用“数量”之类的缩写;你为什么不叫它数字列表或类似的东西

除了特雷的正确答案,还有一些评论:

您的单参数构造函数表示
T顶点
;但是你在那里做了一个“硬”演员。如果T不是字符串,则该代码将抛出异常

因此,您应该要么让AdjList(顺便说一句,这个可怕的名字)像
类AdjList实现了友谊图
;或者,当您不想“修复”字符串的泛型类型时,可以选择
qty=Integer.parseInt(verties.toString())

但是看看这个-听起来不是很奇怪吗?你知道,把一个看起来像数字的东西变成一个字符串,从中解析一个数字?也许它应该一直是一个整数


然后:进行命名。绝对不需要使用“数量”之类的缩写;你为什么不叫它数字列表或类似的东西

提示:阅读java编码指南。字段名以小写字母开头。仅供参考:
要说
还有很长的路要走。提示:阅读java编码指南。字段名以小写字母开头。仅供参考:
要说
FriendShipGraph<String> graph = new AdjList<String>();
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();
    }
}