Adjacency List を使用して 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");
だから誰でもコンストラクタを修正する方法を教えてください。どうもありがとう!