新手在这里!!对于我的项目,我必须从文件中读取数据作为字符串,并为BFS创建一个图形,带有邻接列表。我正在读取文件中的每一行,并将第一个字符串作为键,将下一个字符串作为邻居。但我无法理解,为什么我得到空指针异常。请看看我的代码。我的文件看起来像这样
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)中得到空指针异常错误如何解决问题。有人可以为此提出一些解决方案。
EDIT ..
我也使用getKeyValuePairs()
来查看邻接列表,但是我得到了一些奇怪的输出。任何可能的建议为什么会发生这种情况?
输出:
[city2]
city1 [city2, city2]
city2 [city2, city2, city2, city2]
city3 [city2, city2]
city4 [city2, city2]
city5 [city2, city2]
答案 0 :(得分:2)
因为你的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();
}
}
答案 1 :(得分:1)
您忘了添加到Map
。这是你的代码,带有添加到地图的bugfix:
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();
}
}