Java 从文本文件中提取ArrayList数据

Java 从文本文件中提取ArrayList数据,java,arraylist,dot,Java,Arraylist,Dot,我正在尝试实现Dijkstra算法,该算法读取一个点文件(.txt),并应返回一个映射,首先我必须将文本文件中的数据提取到ArrayList,因此我创建了两个类,一个用于垂直线,另一个用于边 文本文件如下所示: 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

我正在尝试实现Dijkstra算法,该算法读取一个点文件(.txt),并应返回一个映射,首先我必须将文本文件中的数据提取到ArrayList,因此我创建了两个类,一个用于垂直线,另一个用于边

文本文件如下所示:

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

    }
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;
    }
    }
这是我的EdgeStrep课程:

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

    }
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();
    }
}
}
包装实验室;
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
/**
*类导航查找地图上点之间的最短(和/或)路径
*使用Dijkstra算法
*/
公共类导航{
/**
*返回代码:-1如果源不在地图上-2如果目标不在地图上
*如果源点和目标点都不在地图上,则不在地图上-3
*如果在源和目标之间找不到路径,则映射-4
*/
公共静态final int SOURCE_NOT_FOUND=-1;
公共静态final int DESTINATION_NOT_FOUND=-2;
公共静态final int SOURCE\u DESTINATION\u NOT\u FOUND=-3;
公共静态final int NO_PATH=-4;
//添加变量
私有字符串filename=null;//文件名初始化
/**
*构造函数接受一个文件名作为输入,它读取该文件并填充
*节点和边列表中包含相应的节点和边对象
* 
*@param文件名
*包含输入映射的文件的名称
*/
公共导航(字符串文件名){
ArrayList顶点=新的ArrayList();
ArrayList Edge=新的ArrayList();
this.filename=文件名;
试一试{
FileReader fr=新的FileReader(文件名);
BufferedReader in=新的BufferedReader(fr);
字符串行=null;
而((line=in.readLine())!=null){
//在这里,我必须用这些信息填充数组列表
//我需要从文本文件中删除。
}
in.close();
fr.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
我试图找出我实际上应该使用什么来填充文本文件中的ArrayList,但是.split在我的情况下实际上不起作用

谢谢。

使用正则表达式。 首次进口:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
然后写下这样的话:

    String line = null;
    Pattern patternEdge = Pattern.compile("\\s*([A-Z])\\s*->\\s*([A-Z])\\s*\\[label=\"(\\d+),(\\d+)\"\\];");
    Pattern patternVertex = Pattern.compile("\\s*([A-Z])\\s*\\[label=\"([A-Z]),(\\d+)\"\\];");

    Matcher m = null;

    while ((line = in.readLine()) != null) {

                   m = patternEdge.matcher(line);

                  if (m.find()) {
                     String source = m.group(1);
                     String destination = m.group(2);
                     int speedLimit = Integer.parseInt(m.group(3));
                     int distance = Integer.parseInt(m.group(4));

                     Edges.add(new EdgesRep(source, destination, speedLimit, distance));
                  }
                  else {

                    m = patternVertex.matcher(line);

                    if (m.find()) {
                      String name = m.group(2);
                      int time = Integer.parseInt(m.group(3));

                      Vertex.add(new VerticesRep(name, time));
                    }
                    else
                      System.err.println("Expression is incorrect. No matches");

                  }
    }

    //for debugging
    for(EdgesRep e : Edges)
      System.out.println(e.getSource() + " " + e.getDestination() + " " + e.getSpeedLimit() + " " + e.getDistance());

    //for debugging
    for(VerticesRep v : Vertex)
      System.out.println(v.getName() + " " + v.getTime());
因为第一行并不意味着什么,你可以跳过它。。。 在这个代码块上,我假设它以A->B[label=“10,90”]开头; 希望有帮助:)

您可以将regex(es)与组一起使用,或者找到相关子字符串的位置,如“->”或“[label=\”,然后使用
String
子字符串方法自己拆分行。