我将一个txt文件读入Java,并希望将其编码为边,其中每个边都是一对节点(DFS)

我将一个txt文件读入Java,并希望将其编码为边,其中每个边都是一对节点(DFS),java,algorithm,design-patterns,collections,java-8,Java,Algorithm,Design Patterns,Collections,Java 8,文本文件如下所示: 01 02 03 1133 1194 220 2115 2116 5231 所以我想做的是读取我的txt文件并将每一行内容(因为每一条边都由一对节点组成)存储在一个链接列表中,我尝试了以下方法: import java.io.*; import java.util.*; public class readfile{ public Scanner x; public void openFile(){ try{ x = new Scanner(

文本文件如下所示: 01

02

03

1133

1194

220

2115

2116

5231

所以我想做的是读取我的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();
  }
}
import java.io.*;
导入java.util.*;
公共类读取文件{
公共扫描仪x;
公共void openFile(){
试一试{
x=新扫描仪(新文件(“facebook_combined.txt”);
}
捕获(例外e){
System.out.println(“无法打开文件”);
}
}
公共void readFile(){
LinkedList adj=新LinkedList();
while(x.hasNext()){
字符串a=x.next();
int resa=Integer.parseInt(a);
字符串b=x.next();
int resb=Integer.parseInt(b);
加上(resa,resb);
系统输出打印LN(adj);
}
}
公共文件(){
x、 close();
}
}
班长{
公共静态void main(字符串[]args){
readfile r=新的readfile();
r、 openFile();
r、 readFile();
r、 closeFile();
}
}
但它返回一个错误,链表只允许一种类型,这意味着我不能存储一对相互连接的节点

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


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

在Java中,任何
列表都是元素的有序集合;但是这些元素可以是任何类的对象。因此,我们的想法是,您的元素应该是业务逻辑中的关注单元。在您的情况下,这是一对数字。考虑到这一点,您可以创建一个名为
Edge
的自定义类,例如,该类将用作
列表的
元素,并让该
Edge
类存储相应的一对数字。例如:

    static class Edge {
        private int first;
        private int second;

        public Edge(Scanner scanner) {
            this(scanner.nextInt(), scanner.nextInt());
        }

        public Edge(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public int getFirst() {
            return first;
        }

        public int getSecond() {
            return second;
        }

        @Override
        public String toString() {
            return "Edge [first=" + first + ", second=" + second + "]";
        }
    }
然后在代码更改中:

LinkedList<Integer,Integer> adj = new LinkedList<Integer,Integer>();
使
adj
成为
对象的
列表

  • 其他提示:
您还可以利用Java 8流和增强的NIO API,并使用以下简单方法将文本文件的整个转换重写为
列表

    public static List<Edge> readEdges(String filename) throws IOException {
        try (Stream<String> linesStream = Files.lines(Paths.get(filename))) {
            return linesStream
                    .map(line -> new Edge(new Scanner(line)))
                    .collect(Collectors.toList());
        }
    }
公共静态列表readEdges(字符串文件名)引发IOException{
try(Stream-linessstream=Files.lines(path.get(filename))){
回流管流
.map(线->新边(新扫描仪(线)))
.collect(Collectors.toList());
}
}
最后,以下是此方法的完整工作演示,请运行它来感受它:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class EdgesReader {

    public static void main(String[] args) throws IOException {
        List<Edge> edges = readEdges("PATH/TO/MY/EDGES/TEXT/FILE");
        System.out.println(edges);
    }

    public static List<Edge> readEdges(String filename) throws IOException {
        try (Stream<String> linesStream = Files.lines(Paths.get(filename))) {
            return linesStream
                    .map(line -> new Edge(new Scanner(line)))
                    .collect(Collectors.toList());
        }
    }

    static class Edge {
        private int first;
        private int second;

        public Edge(Scanner scanner) {
            this(scanner.nextInt(), scanner.nextInt());
        }

        public Edge(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public int getFirst() {
            return first;
        }

        public int getSecond() {
            return second;
        }

        @Override
        public String toString() {
            return "Edge [first=" + first + ", second=" + second + "]";
        }
    }
}
import java.io.IOException;
导入java.nio.file.Files;
导入java.nio.file.path;
导入java.util.List;
导入java.util.Scanner;
导入java.util.stream.collector;
导入java.util.stream.stream;
公共类边读写器{
公共静态void main(字符串[]args)引发IOException{
List edges=readEdges(“PATH/TO/MY/edges/TEXT/FILE”);
系统输出打印LN(边缘);
}
公共静态列表readEdges(字符串文件名)引发IOException{
try(Stream-linessstream=Files.lines(path.get(filename))){
回流管流
.map(线->新边(新扫描仪(线)))
.collect(Collectors.toList());
}
}
静态类边{
私人int优先;
二等兵;
公共边缘(扫描仪){
这(scanner.nextInt(),scanner.nextInt());
}
公共边缘(整数第一,整数第二){
this.first=first;
这个秒=秒;
}
公共int getFirst(){
先返回;
}
公共int getSecond(){
返回第二;
}
@凌驾
公共字符串toString(){
返回“Edge[first=“+first+”,second=“+second+”];
}
}
}


希望这能有所帮助。

首先,您已经提到了一个错误显示
链接列表只允许一种类型。根本原因是LinkedList支持基于doc java.util.LinkedList的一种类型

要允许LinkedList存储边缘,我们必须提供以下解决方案:

  • 我们可以创建Edge类,该类包含两个整数元素,分别表示from_note和to_节点。 我们知道LinkedList是一个泛型类,LinkedList可以在LinkedList中存储任何类型

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

  • 我们还可以改进代码。例如类命名。您可以遵循GoogleJava风格指南

    当我们学习一个新的类或新的java库时,我们可以通过查看它们的文档来了解该类,从而理解函数和参数的含义

    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();
        }
    }
    
    import java.io.*;
    导入java.util.*;
    班长{
    //解决方案1:通过引入边类
    公共静态类边缘{
    int from;
    int到;
    公共边缘(int-from,int-to){
    this.from=from;
    这个;
    }
    公共字符串toString(){
    从+“->”+返回到;
    }
    }
    公共静态类读取文件{
    公共扫描仪x;
    公共void openFile(){
    试一试{
    x=新扫描仪(新文件(“facebook_combined.txt”);
    }
    捕获(例外e){
    System.out.println(“无法打开文件”);
    }
    }
    public void readFile(){//解决方案1:使用edge类表示边缘
    LinkedList adj=新LinkedList();
    while(x.hasNext()){
    字符串a=x.next();
    int resa=Integer.parseInt(a);
    字符串b=x.next();
    int resb=Integer.pa
    
    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();
        }
    }