我从文件中读取数据时遇到问题。 目前,我正在程序中添加边缘,该程序会根据边缘列表生成一个图形。但是,我希望我的程序从文件中读取边缘以创建图形(逐行读取.txt文件(
.txt文件如下所示:
0, 1
0, 4
1, 2
1, 3
1, 4
2, 3
3, 4
该程序的示例代码如下所示:
import java.util.*;
class GraphIO {
static void addEdge(ArrayList<ArrayList<Integer> > adj,
int u, int v)
{
adj.get(u).add(v);
adj.get(v).add(u);
}
static void printGraph(ArrayList<ArrayList<Integer> > adj)
{
for (int i = 0; i < adj.size(); i++) {
System.out.println("nAdjacency list of vertex" + i);
for (int j = 0; j < adj.get(i).size(); j++) {
System.out.print(" -> "+adj.get(i).get(j));
}
System.out.println();
}
}
public static void main(String[] args)
{
int V = 5;
ArrayList<ArrayList<Integer> > adj
= new ArrayList<ArrayList<Integer> >(V);
for (int i = 0; i < V; i++)
adj.add(new ArrayList<Integer>());
addEdge(adj, 0, 1);
addEdge(adj, 0, 4);
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 3);
addEdge(adj, 3, 4);
printGraph(adj);
}
}
有人有建议吗?
编辑:这次我添加了一个List
,这次正确地调用了addEdge
。抱歉,我上次忽略了方法签名。
在你的while循环中,当你读一个新行时,你可以在解析读取行的数字之后,在那里添加一个边:
while (reader.hasNextLine()) {
String[] nums = reader.nextLine().split(", ");
addEdge(adj, Integer.parseInt(nums[0]), Integer.parseInt(nums[1]));
}
尝试后,您还需要捕获。
旁注:我认为你不应该跳过一行,但我不知道这是否是你的程序应该做的特定事情。
您的addEdge
方法应如下所示,以确保您的List
都充满了ArrayList
并且您不会得到IndexOutOfBoundsException
。
static void addEdge(List<List<Integer>> adj, int u, int v) {
int bigger = (u > v ? u : v) + 1;
//Keep increasing the size until it's right
while (adj.size() < bigger) adj.add(new ArrayList<>());
adj.get(u).add(v);
adj.get(v).add(u);
}
并且您的主要方法必须稍作更改。由于您不知道顶点的数量,因此只需对默认容量进行空ArrayList
。 将adj
声明为List<List<Integer>>
是没有必要的 - 您仍然可以使用ArrayList
,但List
是其他类实现的接口,在这里使用ArrayList
不是绝对必要的,所以我使用了它。
我还删除了reader.nextLine()
,因为您正在丢弃该数据,并且该边缘未被注册。
List<List<Integer>> adj = new ArrayList<>();
File input = new File("filename"); //Define the input file with "filename"
try {
Scanner reader = new Scanner(input); //read in the file
while (reader.hasNextLine()) {
String[] nums = reader.nextLine().split(", ");
addEdge(adj, Integer.parseInt(nums[0]), Integer.parseInt(nums[1]));
}
printGraph(adj);
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
输出:
Adjacency list of vertex0
-> 1 -> 4
Adjacency list of vertex1
-> 0 -> 2 -> 3 -> 4
Adjacency list of vertex2
-> 1 -> 3
Adjacency list of vertex3
-> 1 -> 2 -> 4
Adjacency list of vertex4
-> 0 -> 1 -> 3
但是,该程序非常依赖于txt
文件中边缘的顺序,所以我宁愿这样做。它不是很有效,它使用地图和集合而不是ArrayLists,但对我来说感觉更清晰。
static void addEdge(Map<Integer, Set<Integer>> adj, int u, int v) {
//If it doesn't already exist, add the set of adjacent vertices here
if (!adj.containsKey(u)) adj.put(u, new HashSet());
if (!adj.containsKey(v)) adj.put(v, new HashSet());
adj.get(u).add(v);
adj.get(v).add(u);
}
static void printGraph(Map<Integer, Set<Integer>> adj) {
for (int u : adj.keySet()) {
System.out.println("nAdjacency list of vertex" + u);
for (int v : adj.get(u)) {
System.out.print(" -> " + v);
}
System.out.println();
}
}
public static void main(String[] args) {
Map<Integer, Set<Integer>> adj = new HashMap<>();
File input = new File("filename");
try {
Scanner reader = new Scanner(input); //read in the file
while (reader.hasNextLine()) {
String[] nums = reader.nextLine().split(", ");
addEdge(adj, Integer.parseInt(nums[0]), Integer.parseInt(nums[1]));
}
printGraph(adj);
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}