从文件中读取字符串并为图创建邻接表

Reading strings from a file and creating Adjacency lists for Graphs

这里是新手!!对于我的项目,我必须从文件中读取数据作为字符串并为 BFS 创建一个图形,邻接 list.I 从文件中读取每一行并将第一个字符串作为键,将下一个字符串作为它的邻居。但是我不明白,为什么我得到空指针 exception.Please 看看我的代码。我的文件看起来像这样

city.txt

city1  city2   10
city1  city3   15
city2  city1   5
city2  city3   6 
city2  city4   22
city2  city5   1
city3  city4   9
city3  city5   16
city4  city1   4
city4  city2   8
city5  city2   1
city5  city3   13

我的 Graph.java 文件是

import java.io.*;
import java.util.*;

public class Graph {

    Map<String, LinkedList<String>> adj;

    public Graph() {
    // TODO Auto-generated constructor stub
    }

    public Graph(String[] nodes) 
    {
       adj = new HashMap<String, LinkedList<String>>();
       for (int i = 0; i < nodes.length; ++i) 
       {
          adj.put(nodes[i], new LinkedList<String>());
       }
    }

    public void addNeighbor(String v1,String v2) {
       adj.get(v1).add(v2);
    }

    public List<String> getNeighbors(String v) {
       return adj.get(v);
    }

    public void getKeyValuePairs()
    {
        Iterator iterator = adj.keySet().iterator();

        while (iterator.hasNext()) {
           String key = iterator.next().toString();
           LinkedList<String> value = adj.get(key); 
           System.out.println(key + " " + value);
        }
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("city.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line = br.readLine();
        String [] tokens = line.split("\s+");
        String [] nodes = new String[tokens.length];
        for (int i = 0; i < nodes.length; ++i) {
           nodes[i] = tokens[i];
        }

        Graph g = new Graph(nodes);
        String var_1 = tokens[0];
        String var_2 = tokens[1];
        //String var_3 = tokens[2];



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

           tokens = line.split("\s+");
           nodes = new String[tokens.length];
           for (int i = 0; i < nodes.length; ++i) {
              nodes[i] = tokens[i];
           }

           var_1 = tokens[0];
           var_2 = tokens[1];
           //String var_3 = tokens[2];

           g.addNeighbor(var_1, var_2);

        }
        g.getKeyValuePairs();
        br.close();
     }

  }

我在行 g.addNeighbor(var_1, var_2) 中收到空指针异常错误,我该如何解决该问题。有人可以为此提出一些解决方案吗?

编辑..

我还使用 getKeyValuePairs() 查看邻接表,但我得到了一些奇怪的输出。任何可能的建议为什么会发生?

输出:

 [city2]
city1 [city2, city2]
city2 [city2, city2, city2, city2]
city3 [city2, city2]
city4 [city2, city2]
city5 [city2, city2]

因为您的 HashMap adj 不包含顶点 var_1。您只需将第一行的顶点添加到图中。

编辑:下面是一个工作示例:

public class Graph
{
    Map<String, LinkedList<String>> adj;

    public Graph() {
        adj = new HashMap<String, LinkedList<String>>();
    }

    public void addNode(String node) 
    {
        adj.putIfAbsent(node, new LinkedList<String>());
    }

    public void addNeighbor(String v1,String v2) {
       adj.get(v1).add(v2);
    }

    public List<String> getNeighbors(String v) {
       return adj.get(v);
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("city.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line;
        String [] tokens;

        Graph g = new Graph();
        while( (line = br.readLine()) != null )
        {
           tokens = line.split("\s+");
           g.addNode( tokens[0]);
           g.addNode( tokens[1]);
           g.addNeighbor( tokens[0], tokens[1]);
        }
        br.close();
     }
}

您忘记添加到您的Map。这是您的代码,其中包含添加到地图的错误修正:

if(adj.get(v1) == null) {
   adj.put(v1, new LinkedList<String>());
}

完整代码

import java.io.*;
import java.util.*;

public class Main {

    private Map<String, LinkedList<String>> adj;

    public Main() {
        // TODO Auto-generated constructor stub
    }

    public Main(String[] nodes)
    {
        adj =new HashMap<String, LinkedList<String>>();
        System.out.println("nodes" + nodes.length);
        for (int i = 0; i < nodes.length; ++i)
        {
            adj.put(nodes[i], new LinkedList<String>());
        }
    }

    public void addNeighbor(String v1,String v2) {
        if(adj.get(v1) == null) {
            adj.put(v1, new LinkedList<String>());
        }
        adj.get(v1).add(v2);
    }

    public List<String> getNeighbors(String v) {
        return adj.get(v);
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("city.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line = br.readLine();
        String [] tokens = line.split("\s+");
        String [] nodes = new String[tokens.length];
        for (int i = 0; i < nodes.length; ++i) {
            nodes[i] = tokens[i];
        }

        Main g = new Main(nodes);
        String var_1 = tokens[0];
        String var_2 = tokens[1];
        //String var_3 = tokens[2];

        while( (line = br.readLine()) != null )
        {
            System.out.println(" test " + line);
            tokens = line.split("\s+");
            nodes = new String[tokens.length];
            for (int i = 0; i < nodes.length; ++i) {
                nodes[i] = tokens[i];
            }
            var_1 = tokens[0];
            var_2 = tokens[1];
            g.addNeighbor(var_1, var_2);

        }
        br.close();
    }

}