【问题标题】:create adjacency list from a file从文件创建邻接列表
【发布时间】:2013-11-24 08:51:15
【问题描述】:
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

我想从文件条目创建邻接列表。我的代码给出了邻接矩阵。我听说java中有特定的邻接列表功能请帮助

【问题讨论】:

    标签: java file adjacency-list adjacency-matrix


    【解决方案1】:

    您可能想从下载Open Data Structures 开始。如果您单击标记为java sources 的链接,您将获得一个存档文件,其中包含几个数据结构的源(和编译的类文件)。其中包括 AdjcencyLists 的 java 实现。

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 2010-11-10
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多