我有一個圖的鄰接矩陣的file.how這個鄰接MATIX存儲在一個二維矩陣 我輸入文件看起來像如何存儲文件項,鄰接表
e 1 36
e 2 45
e 3 74
e 4 18
e 5 36
e 6 74
e 6 45
e 6 136
e 6 36
e 6 21
e 6 18
e 7 18
e 7 116
e 7 74
e 7 99
e 7 81
e 7 135
我需要一個輸出作爲鄰接表:
1-->36
2-->45
3-->74
4-->18
5-->36
6-->74-->45-->136-->36-->21-->18
7-->18-->116--->74-->99-->81-->135
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.StringTokenizer;
public class Graph1 {
public static void main(String[] args) throws FileNotFoundException {
int linecount = 0, ec = 0;
String nbin = null, cbin = null;
int[][] data = null;
String e = "e";
System.out.println("Graph Coloring Algorithm Test\n");
Scanner sc = new Scanner(System.in);
System.out.print("Enter graph input file name: ");
String newfile = sc.nextLine() + ".txt";
File file = new File(newfile);
Scanner scan = new Scanner(file);
while ((scan.hasNext())) {
StringTokenizer t = new StringTokenizer(scan.nextLine());
if (t.nextToken().equals(e)) {
ec++;
nbin = scan.nextInt();
cbin = scan.nextInt();
}
linecount++;
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 5; ++j) {
{
data[nbin][cbin] = 1;
}
}
}
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 5; ++j) {
{
System.out.print(data[i][j]);
}
}
}
}
這代碼有錯誤。如何將字符串標記轉換爲整數 我該如何接受文件中以e開頭的行並將其添加到鄰接列表中。
告訴我們你試過的東西。 – Linus
如何分離以e開頭的數據條目? –
您嘗試過的任何內容對我們來說都是有用的,可以給您一個答案。 – Linus