0

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");

だから誰でもコンストラクタを修正する方法を教えてください。どうもありがとう!

4

2 に答える 2

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

には、引数のないコンストラクターがありませんAdjJust。で行ったように、独自のコンストラクターを提供する場合、デフォルトのゼロ引数コンストラクターは生成されませんAdjList(T vertices)

デフォルトのコンストラクターを提供する必要があります。表示されていない他のコードによっては、次のようなもので十分な場合があります。

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();
    }
}

数量を表す文字列を渡す理由は正確にはわかりませんが、これにより、少なくともあなたが求めているコンパイルエラーが修正されるはずです。

于 2016-08-31T03:15:39.717 に答える