这是我的输入文件:
4 4 3 2 //edges//vertex//must visit node//case
1 2 4// this are the graph node and its a directed weighted grap//
1 3 20
2 3 4
3 4 4
1 4// source 1 to 4//
2 4 // source 2 to 4//
如何读取输入文件? 1.对于第一行用户,给出顶点,边,必须访问节点,案例 2。 3.那条线代表它们之间的图顶点,边和权重 4。 5, 使用源1和目标4的dijkstra算法 最后两行 6。
7。 我也上传了新的经过修改的代码,也遇到问题了
import java.util.*;
import java.io.*;
public class Adjacency_matrix {
/**
* @param args the command line arguments
*/
static int a[][];
private static boolean[] checks;
public static void main(String[] args) {
// TODO code application logic here
try {
FileReader f = new FileReader("E:\\dijkstra.txt");
BufferedReader b = new BufferedReader(f);
// int[]v=new int[4];
String[] v = b.readLine().split(" ");
for (int i = 0; i < v.length; i++) {
int ver = Integer.parseInt(v[0]);
int edg = Integer.parseInt(v[1]);
int x = Integer.parseInt(v[2]);
int test = Integer.parseInt(v[3]);
a = new int[ver][ver];
}
String[] v1 = b.readLine().split(" ");
for (int i = 0; i < 4; i++) {
int ver1 = Integer.parseInt(v1[0]);
int edg1 = Integer.parseInt(v1[1]);
int w = Integer.parseInt(v1[2]);
a[ver1][edg1] = w;
}
String s;
while ((s = b.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s, " ");
int src = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
}
System.out.println("Adjacency Matrix");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
答案 0 :(得分:2)
catch
块更改为e.printStackTrace()
,以查看程序崩溃的位置。e
(您定义的对象,不是例外)或c
。那可能是因为您没有提供全部内容。c
的赋值,数组分配失败,因为您基于token1
和token2
分配索引,并假设基于1的数组而不是基于0的数组,因此它将为输入行ArrayIndexOutOfBoundsException
抛出3 4 4
1 4
相关的成本,但是您的while
条件将继续添加图形元素,直到没有更多输入为止。