我需要從包含多個「迷宮」表示爲鄰接表的文本文件做一個曲線圖。列表如下:創建從一個文本文件中的迷宮圖:Java的
A,G
A,B,F
B,A,C,G
C,B,D,G
D,C,E
E,D,F,G
F,A,E
G,B,C,E
D,F
A,B,G
B,A,C,E,G
C,B,D,E,G
D,C
E,B,C,G
F,G
G,A,B,C,E,F
F,A
A,B,G
B,A,G
C,D,G
D,C,G
E,F,G
F,E,G
G,A,B,C,D,E,F
每個「迷宮」的第一行包含迷宮(第一個字母)和迷宮(第二個字母)的結束節點的起始節點。
我分析的文本文件導入所有行(包括空行)的ArrayList,遂成線的ArrayLists的ArrayList(獨立迷宮的列表)。我通過將整行文本分成空白行來做到這一點。我現在的問題是,我無法弄清楚如何使用我的節點類,從這些「迷宮」構造圖。這裏是我的節點類:
package algo2;
import java.util.ArrayList;
public class Node<T>{
private T value; //this Node<T>'s value
public ArrayList<Node<T>> connections;
public boolean isStart;
public boolean isEnd;
public boolean visited;
public Node(T value){
this.value = value;
connections = new ArrayList<Node<T>>();
}
//returns the value of this Node<T>
public T getValue(){
return value;
}
//returns true if the node is connected to any other nodes
public boolean isConnected(){
if(connections == null){
return false;
}
return true;
}
//returns a list of nodes connected to this node
public ArrayList<Node<T>> getConnections(){
return connections;
}
//sets the node's value
public void setValue(T value){
this.value = value;
}
//adds a connection from this node to the passed node
public void connect(Node<T> node){
connections.add(node);
}
public String toString(){
return value+"";
}
}
有人可以指出我在正確的方向嗎?