Java 使用扫描器和字符串标记器读取文件并检索邻接列表
我的输入文件是:Java 使用扫描器和字符串标记器读取文件并检索邻接列表,java,file,java.util.scanner,stringtokenizer,Java,File,Java.util.scanner,Stringtokenizer,我的输入文件是: 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
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;
int nbin = 0, cbin = 0;
int[][] data = new int[6][6];
String e = "e";
Scanner sc = new Scanner(System.in);
System.out.print("Enter 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++;System.out.print("ec.." + ec );
nbin = Integer.parseInt(t.nextToken());
cbin = Integer.parseInt(t.nextToken());
data[nbin][cbin] = 1;
}
}
这里生成了邻接矩阵。我已更新了文件。我想将文件条目转换为邻接列表更改您的
数据
声明
jjjjjjjjj
e 1 3
e 2 5
e 3 4
e 4 1
e 5 3
到
因为,atcbin=5
int[][] data = new int[6][6];
我需要把这些数据转换成邻接列表。你知道吗?
int[][] data = new int[6][6];
data[nbin][cbin] = 1;// ArrayIndexOutOfBoundsException.