我正在做一个关于使用邻接列表实现 Graph 的教程任务,但构造函数有问题。
在给定的GraphTester.java
我有:
//Constructor cannot be applied to given types
FriendShipGraph<String> graph = new AdjList<String>();
然后FriendShipGraph.java
提供了一个接口:
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
}
所以我需要编写一个类来实现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
来实现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();
}
}
但是,当我编写自己的测试文件时,我会像这样创建 AdjList 对象而不会出错,但这不是该类所需要的:
AdjList<String> aList = new AdjList<String>("9");
所以任何人都请建议我如何修复构造函数。非常感谢!