【发布时间】:2014-05-26 03:45:04
【问题描述】:
我正在尝试实现读取 DOT 文件 (.txt) 并返回地图的 Dijkstra 算法,首先我必须将数据从文本文件提取到 ArrayList,因此我为 Verticles 创建了两个类,以及边缘的另一个类。
文本文件如下所示:
Digraph {
A -> B [label="10,90"];
A -> C [label="8,80"];
C -> B [label="1,50"];
B -> D [label="7,60"];
C -> D [label="6,80"];
D -> E [label="4,90"];
E -> F [label="2,130"];
D -> F [label="5,130"];
F -> G [label="5,120"];
G -> H [label="5,100"];
A [label="A,5"];
B [label="B,4"];
C [label="C,3"];
D [label="D,2"];
E [label="E,1"];
F [label="F,6"];
G [label="G,7"];
H [label="H,8"];
}
例如,在第 1 行中,我应该提取“A”的源和“B”的目标以及 10 的速度限制和 90 的距离,这与边缘有关。从第 11 行我应该提取名称和时间,例如在第 11 行中,名称应该是“A”,时间应该是 5。
这是我简单的前两个类:
VerticlesRep 类:
package lab;
public class VerticesRep {
private String name;
private int time;
public VerticesRep(String name, int time) {
this.name = name;
this.time = time;
}
public String getName() {
return name;
}
public int getTime() {
return time;
}
public void setName(String name) {
this.name = name;
}
public void setTime(int time) {
this.time = time;
}
}
这是我的 EdgesRep 课程:
package lab;
public class EdgesRep {
private String Source;
private String Destination;
private int speedLimit;
private int distance;
public EdgesRep(String Source, String Destination, int speedLimit, int distance) {
this.setSource(Source);
this.setDestination(Destination);
this.setSpeedLimit(speedLimit);
this.setDistance(distance);
}
public String getSource() {
return Source;
}
public void setSource(String source) {
Source = source;
}
public String getDestination() {
return Destination;
}
public void setDestination(String destination) {
Destination = destination;
}
public int getSpeedLimit() {
return speedLimit;
}
public void setSpeedLimit(int speedLimit) {
this.speedLimit = speedLimit;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
}
这是导航类:
package lab;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* The class Navigation finds the shortest (and/or) path between points on a map
* using the Dijkstra algorithm
*/
public class Navigation {
/**
* Return codes: -1 if the source is not on the map -2 if the destination is
* not on the map -3 if both source and destination points are not on the
* map -4 if no path can be found between source and destination
*/
public static final int SOURCE_NOT_FOUND = -1;
public static final int DESTINATION_NOT_FOUND = -2;
public static final int SOURCE_DESTINATION_NOT_FOUND = -3;
public static final int NO_PATH = -4;
// added variables
private String filename = null; // filename initialization
/**
* The constructor takes a filename as input, it reads that file and fill
* the nodes and edges Lists with corresponding node and edge objects
*
* @param filename
* name of the file containing the input map
*/
public Navigation(String filename) {
ArrayList<VerticesRep> Vertex = new ArrayList<VerticesRep>();
ArrayList<EdgesRep> Edges = new ArrayList<EdgesRep>();
this.filename = filename;
try {
FileReader fr = new FileReader(filename);
BufferedReader in = new BufferedReader(fr);
String line = null;
while ((line = in.readLine()) != null) {
// here i must fill the ArrayLists with the Information
// I need from the text file.
}
in.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我试图找出我实际上应该使用什么来从文本文件中填充 ArrayList,但 .split 在我的情况下实际上不起作用。
谢谢。
【问题讨论】:
-
您可以将正则表达式与组一起使用,或者找出相关子字符串的位置,例如“->”或“[label=\””,然后使用
substring方法自行拆分行String.