Java 将txt文件转换为邻接列表,然后生成图形

Java 将txt文件转换为邻接列表,然后生成图形,java,text,java.util.scanner,Java,Text,Java.util.scanner,我正在做一个广度优先搜索程序,它将读取邻接列表类型的文本文件,然后相应地对其执行BFS。我无法读取文本文件,然后将其添加为节点数组列表 我怎样才能让它正确地阅读每一行并关联它的连接 我已经通过在主节点中手动添加节点来测试我的程序,然后制作一个图并在其上执行BFS 这是我的节点类: import java.util.*; public class Node { public String data; // data element public boolean

我正在做一个广度优先搜索程序,它将读取邻接列表类型的文本文件,然后相应地对其执行BFS。我无法读取文本文件,然后将其添加为节点数组列表

我怎样才能让它正确地阅读每一行并关联它的连接

我已经通过在主节点中手动添加节点来测试我的程序,然后制作一个图并在其上执行BFS

这是我的节点类:

import java.util.*;

public class Node {

        public String data; // data element
        public boolean visited=false; // flag to track the already visited node
        public List<Node> adjacentNodes = new LinkedList<Node>(); // adjacency list
//      public List adjacentNodes = new LinkedList(); // adjacency list
        public Node rootNode;

        public Node(String data){
            this.data = data;
        }

        public void addAdjacentNode(final Node node){
            adjacentNodes.add(node);
            node.adjacentNodes.add(this);
//          adjacentNodes.add(rootNode);
//          node.adjacentNodes.add(this)
        }

    }
下面是我在main中自己编写的代码块:

        Scanner scan = new Scanner(new File("Connections.txt"));   // scanner to read file
        String line = scan.nextLine();                        // read first line
        int nbLine = Integer.parseInt(line);                  // get number of lines
        ArrayList<int[]> al = new ArrayList<int[]>();        
        for(int i = 0; i < nbLine; i++) {                     // read each line
           line = scan.nextLine();
           String[] token = line.split(" ");                  // split each number into different String
           int[] points = new int[token.length - 1];          // prepare array of int[] - 1
//         int[] point = new int[];
           int[] point;
        for(int j = 0; j < token.length; j++){              // skip first one
             points[j-1] = Integer.parseInt(token[j]);       // store as int
           al.add(points);                                    // save in ArrayList
        }
Scanner scan=new Scanner(新文件(“Connections.txt”);//扫描仪读取文件
String line=scan.nextLine();//读第一行
int nbLine=Integer.parseInt(line);//获取行数
ArrayList al=新的ArrayList();
对于(inti=0;i

我走对了吗?

你的基本步骤需要是:

  • 读取文件的一行
  • 把那条线变成一个物体
  • 将该对象添加到集合中
  • 重复,直到你没有更多的行要读
  • 既然我不想为你做你的工作,我就给你留一些样品:

    读取文件的一行

    String line = reader.readLine(); // in this case, 'reader' will be a BufferedReader referencing your file
    
    将该线变成一个对象

    这取决于您的输入文件格式。例如,如果我的输入是这样的:

    first_thing 10
    second_thing 20
    third_thing 30
    ...
    
    然后我可以做:

    String[] components = line.split(" ");
    if (components.length == 2) {
        MyCustomObject myCustomObject = new MyCustomObject(components[0], components[1]);
    }
    
    将该对象添加到集合中

    String[] components = line.split(" ");
    if (components.length == 2) {
        MyCustomObject myCustomObject = new MyCustomObject(components[0], components[1]);
        myCollection.add(myCustomObject); // you can choose the type of collection here
    }
    
    重复,直到没有更多的行可读

    while ( (line = reader.readLine()) != null ) {
        ...
    } 
    

    希望这能有所帮助!

    输入文件是什么样子的?到目前为止您尝试了什么,遇到了什么具体问题?实际行为与预期行为有何不同?@JasonC我更新了代码并添加了输入文件,以及我编写的一段代码,并进行了尝试,其中一行出现了问题,尽管如前所述boveUpdate!我消除了那个错误我更新了我的代码,并根据我在网上的理解添加了一些代码,你能看一下吗?
    String[] components = line.split(" ");
    if (components.length == 2) {
        MyCustomObject myCustomObject = new MyCustomObject(components[0], components[1]);
        myCollection.add(myCustomObject); // you can choose the type of collection here
    }
    
    while ( (line = reader.readLine()) != null ) {
        ...
    }