首页 > 解决方案 > 我将一个 txt 文件读入 Java,我想让它被编码为边,其中每个边都是一对节点(DFS)

问题描述

文本文件看起来像:0 1

0 2

0 3

1 133

1 194

2 20

2 115

2 116

5 231……

所以我想做的是读取我的 txt 文件并将每一行内容(因为每条边由一对节点组成)存储在一个链表中,我尝试了这个:

import java.io.*;
import java.util.*;


public class readfile{



 public Scanner x;
  public void openFile(){
    try{
      x = new Scanner(new File("facebook_combined.txt"));
    }

catch(Exception e){
  System.out.println("could not open file");
}
}

  public void readFile(){
    LinkedList<Integer,Integer> adj = new LinkedList<Integer,Integer>();
    while(x.hasNext()){
      String a = x.next();
      int resa = Integer.parseInt(a);       
      String b = x.next();
      int resb = Integer.parseInt(b);
      adj.add(resa, resb);
      System.out.println(adj);
    }
  }

  public void closeFile(){
    x.close();
  }
}
class Main {
  public static void main(String[] args) {
    readfile r = new readfile();
    r.openFile();
    r.readFile();
    r.closeFile();
  }
}

但是它返回了一个错误,即链表只允许一种类型,这意味着我无法存储一对相互连接的节点。

那么有没有其他方法来存储边(一对节点)?

我是java领域的一个真正的新手,我正在努力更好地学习这门语言,如果有任何建议,我将不胜感激!

标签: javaalgorithmdesign-patternscollectionsjava-8

解决方案


首先,您已经提到了错误显示linked-list only allow one type。根本原因是 LinkedList 支持基于文档 java.util.LinkedList https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html的一种类型

为了允许 LinkedList 存储边,我们必须解决方案:

  1. 我们可以创建包含两个整数元素的 Edge 类,它们分别代表 from_note 和 to_node。而且我们知道 LinkedList 是一个泛型类,LinkedList 可以在 LinkedList 中存储任何类型。

  2. 我们可以使用 int[] 来表示边缘。并且数组中的第一个元素是from_note,数组中的第二个元素是to_node。

还有其他我们可以改进代码的地方。比如类命名。你可以按照谷歌 Java 风格指南https://google.github.io/styleguide/javaguide.html

当我们学习一个新的类或新的 Java 库时,我们可以通过查看他们的文档(如https://docs.oracle.com )来复习该类,以了解函数和参数的含义。

import java.io.*;
import java.util.*;

class Main {
    // solution 1: by introduce Edge class
    public static class Edge{
        int from;
        int to;
        public Edge(int from, int to){
            this.from = from;
            this.to = to;
        }

        public String toString(){
            return from+"->"+to;
        }
    }

    public static class readfile{
        public Scanner x;
        public void openFile(){
            try{
                x = new Scanner(new File("facebook_combined.txt"));
            }

            catch(Exception e){
                System.out.println("could not open file");
            }
        }

        public void readFile(){ // solution 1: represent edge by using Edge class
            LinkedList<Edge> adj = new LinkedList<Edge>();
            while(x.hasNext()){
                String a = x.next();
                int resa = Integer.parseInt(a);
                String b = x.next();
                int resb = Integer.parseInt(b);
                Edge edge = new Main.Edge(resa, resb);
                adj.add(edge);
                System.out.println(edge);
            }
        }

        public void readFile1(){ //solution 2: represent edge by using array
            LinkedList<int[]> adj = new LinkedList<int[]>();
            while(x.hasNext()){
                String a = x.next();
                int resa = Integer.parseInt(a);
                String b = x.next();
                int resb = Integer.parseInt(b);
                int[] edge = new int[]{resa, resb};
                adj.add(edge);
                System.out.println(edge[0] + "->"+ edge[1]);
            }
        }

        public void closeFile(){
            x.close();
        }
    }

    public static void main(String[] args) {
        readfile r = new readfile();
        r.openFile();
        r.readFile();
        r.closeFile();
    }
}

推荐阅读