java中从文本文件到对象数组的读取

java中从文本文件到对象数组的读取,java,Java,我正在研究Dijkstra算法。我从文本文件中读取数据的程序。如果我在程序中输入关系,程序工作正常,但当我试图从文本文件读取输入时,它不工作 程序本身中的程序输入如下: private static final Graph.Edge[] Arr = { new Graph.Edge("a", "b", 1), new Graph.Edge("a", "c", 1), new Graph.Edge("a", "f", 1),

我正在研究Dijkstra算法。我从文本文件中读取数据的程序。如果我在程序中输入关系,程序工作正常,但当我试图从文本文件读取输入时,它不工作

程序本身中的程序输入如下:

 private static final Graph.Edge[] Arr = {
          new Graph.Edge("a", "b", 1),
          new Graph.Edge("a", "c", 1),
          new Graph.Edge("a", "f", 1),
          new Graph.Edge("b", "c", 1),
          new Graph.Edge("b", "d", 1),
          new Graph.Edge("c", "d", 1),
          new Graph.Edge("c", "f", 1),
          new Graph.Edge("d", "e", 1),
          new Graph.Edge("e", "f", 1),
       };
public static Graph.Edge[] readTextFile(String fileName) {

    String line = null;
    Graph.Edge[] Gr=new Graph.Edge[Dijkstra.count-1];
    try {
        FileReader fileReader = new FileReader("txt2.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        int i=0;
        while ((line = bufferedReader.readLine()) != null) {
            String[] tokens = line.split("\\t+");
            String s = tokens[0];
            String e = tokens[2];
            Gr[i] =new Graph.Edge(s, e, 1);
            i=i+1;
        }

        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");
    } catch (IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");
    }
    return Gr;
    }
package shortestPath;
import java.util.Scanner;
import java.io.*;
import java.util.*;

public class Dijkstra {

/*private static final Graph.Edge[] Arr = {
          new Graph.Edge("a", "b", 1),
          new Graph.Edge("a", "c", 1),
          new Graph.Edge("a", "f", 1),
          new Graph.Edge("b", "c", 1),
          new Graph.Edge("b", "d", 1),
          new Graph.Edge("c", "d", 1),
          new Graph.Edge("c", "f", 1),
          new Graph.Edge("d", "e", 1),
          new Graph.Edge("e", "f", 1),
    };*/
    public static int count;
    //public static Graph.Edge[] GRAPH = new Graph.Edge[count] ;

    public static void countLines(String file) throws IOException
    {
    LineNumberReader  lnr = new LineNumberReader(new FileReader(new File(file)));
    lnr.skip(Long.MAX_VALUE);
    Dijkstra.count=lnr.getLineNumber() + 1; //Add 1 because line index starts at 0
    // Finally, the LineNumberReader object should be closed to prevent resource leak
    lnr.close();
    //return Dijkstra.count;
    }

    public static Graph.Edge[] readTextFile(String fileName) {

    String line = null;
    Graph.Edge[] Gr=new Graph.Edge[Dijkstra.count-1];
    try {
        FileReader fileReader = new FileReader("txt2.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        int i=0;
        while ((line = bufferedReader.readLine()) != null) {
            String[] tokens = line.split("\\t+");
            String s = tokens[0];
            String e = tokens[2];
            Gr[i] =new Graph.Edge(s, e, 1);
            i=i+1;
        }

        // Always close files.
        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");
    } catch (IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");
    }
    //return Dijkstra.GRAPH;
    return Gr;
    }


       private static final String START = "12";
       private static final String END = "18";

       public static void main(String[] args) throws IOException {
          countLines("hsa00072.txt"); 
          Graph.Edge[] GRAPH=readTextFile("hsa00072.txt");
          Graph g = new Graph(GRAPH);
          g.dijkstra(START);
          g.printPath(END);


          //g.printAllPaths();
       }
    }

    class Graph {
       private final Map<String, Vertex> graph; // mapping of vertex names to Vertex objects, built from a set of Edges

       /** One edge of the graph (only used by Graph constructor) */
       public static class Edge {
          public final String v1, v2;
          public final int dist;
          public Edge(String v1, String v2, int dist) {
             this.v1 = v1;
             this.v2 = v2;
             this.dist = dist;
          }
       }

       /** One vertex of the graph, complete with mappings to neighbouring vertices */
       public static class Vertex implements Comparable<Vertex> {
          public final String name;
          public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity
          public Vertex previous = null;
          public final Map<Vertex, Integer> neighbours = new HashMap<>();

          public Vertex(String name) {
             this.name = name;
          }

          private void printPath() {
             if (this == this.previous) {
                System.out.printf("%s", this.name);
             } else if (this.previous == null) {
                System.out.printf("%s(unreached)", this.name);
             } else {
                this.previous.printPath();
                System.out.printf(" -> %s(%d)", this.name, this.dist);
             }
          }

          public int compareTo(Vertex other) {
             return Integer.compare(dist, other.dist);
          }
       }

       /** Builds a graph from a set of edges */
       public Graph(Edge[] edges) {
          graph = new HashMap<>(edges.length);

          //one pass to find all vertices
          for (Edge e : edges) {
             if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1));
             if (!graph.containsKey(e.v2)) graph.put(e.v2, new Vertex(e.v2));
          }

          //another pass to set neighbouring vertices
          for (Edge e : edges) {
             graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist);
             //graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also do this for an undirected graph
          }
       }

       /** Runs dijkstra using a specified source vertex */ 
       public void dijkstra(String startName) {
          if (!graph.containsKey(startName)) {
             System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName);
             return;
          }
          final Vertex source = graph.get(startName);
          NavigableSet<Vertex> q = new TreeSet<>();

          // set-up vertices
          for (Vertex v : graph.values()) {
             v.previous = v == source ? source : null;
             v.dist = v == source ? 0 : Integer.MAX_VALUE;
             q.add(v);
          }

          dijkstra(q);
       }

       /** Implementation of dijkstra's algorithm using a binary heap. */
       private void dijkstra(final NavigableSet<Vertex> q) {      
          Vertex u, v;
          while (!q.isEmpty()) {

             u = q.pollFirst(); // vertex with shortest distance (first iteration will return source)
             if (u.dist == Integer.MAX_VALUE) break; // we can ignore u (and any other remaining vertices) since they are unreachable

             //look at distances to each neighbour
             for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
                v = a.getKey(); //the neighbour in this iteration

                final int alternateDist = u.dist + a.getValue();
                if (alternateDist < v.dist) { // shorter path to neighbour found
                   q.remove(v);
                   v.dist = alternateDist;
                   v.previous = u;
                   q.add(v);
                } 
             }
          }
       }

       /** Prints a path from the source to the specified vertex */
       public void printPath(String endName) {
          if (!graph.containsKey(endName)) {
             System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName);
             return;
          }

          graph.get(endName).printPath();
          System.out.println();
       }
       /** Prints the path from the source to every vertex (output order is not guaranteed) */
       public void printAllPaths() {
          for (Vertex v : graph.values()) {
             v.printPath();
             System.out.println();
          }
       }

    }
12  ECrel   15
15  ECrel   18
11  ECrel   12
12  ECrel   14
11  ECrel   14
11  ECrel   18
14  maplink 17
我尝试从文本文件中读取的内容,而不是此输入,如下所示: 首先,我将行数计算为数组的大小:

public static int count;
public static void countLines(String file) throws IOException
    {
    LineNumberReader  lnr = new LineNumberReader(new FileReader(new File(file)));
    lnr.skip(Long.MAX_VALUE);
    Dijkstra.count=lnr.getLineNumber() + 1; 
        lnr.close();
}
读取文本文件并将数据保存到数组的函数如下所示:

 private static final Graph.Edge[] Arr = {
          new Graph.Edge("a", "b", 1),
          new Graph.Edge("a", "c", 1),
          new Graph.Edge("a", "f", 1),
          new Graph.Edge("b", "c", 1),
          new Graph.Edge("b", "d", 1),
          new Graph.Edge("c", "d", 1),
          new Graph.Edge("c", "f", 1),
          new Graph.Edge("d", "e", 1),
          new Graph.Edge("e", "f", 1),
       };
public static Graph.Edge[] readTextFile(String fileName) {

    String line = null;
    Graph.Edge[] Gr=new Graph.Edge[Dijkstra.count-1];
    try {
        FileReader fileReader = new FileReader("txt2.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        int i=0;
        while ((line = bufferedReader.readLine()) != null) {
            String[] tokens = line.split("\\t+");
            String s = tokens[0];
            String e = tokens[2];
            Gr[i] =new Graph.Edge(s, e, 1);
            i=i+1;
        }

        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");
    } catch (IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");
    }
    return Gr;
    }
package shortestPath;
import java.util.Scanner;
import java.io.*;
import java.util.*;

public class Dijkstra {

/*private static final Graph.Edge[] Arr = {
          new Graph.Edge("a", "b", 1),
          new Graph.Edge("a", "c", 1),
          new Graph.Edge("a", "f", 1),
          new Graph.Edge("b", "c", 1),
          new Graph.Edge("b", "d", 1),
          new Graph.Edge("c", "d", 1),
          new Graph.Edge("c", "f", 1),
          new Graph.Edge("d", "e", 1),
          new Graph.Edge("e", "f", 1),
    };*/
    public static int count;
    //public static Graph.Edge[] GRAPH = new Graph.Edge[count] ;

    public static void countLines(String file) throws IOException
    {
    LineNumberReader  lnr = new LineNumberReader(new FileReader(new File(file)));
    lnr.skip(Long.MAX_VALUE);
    Dijkstra.count=lnr.getLineNumber() + 1; //Add 1 because line index starts at 0
    // Finally, the LineNumberReader object should be closed to prevent resource leak
    lnr.close();
    //return Dijkstra.count;
    }

    public static Graph.Edge[] readTextFile(String fileName) {

    String line = null;
    Graph.Edge[] Gr=new Graph.Edge[Dijkstra.count-1];
    try {
        FileReader fileReader = new FileReader("txt2.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        int i=0;
        while ((line = bufferedReader.readLine()) != null) {
            String[] tokens = line.split("\\t+");
            String s = tokens[0];
            String e = tokens[2];
            Gr[i] =new Graph.Edge(s, e, 1);
            i=i+1;
        }

        // Always close files.
        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");
    } catch (IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");
    }
    //return Dijkstra.GRAPH;
    return Gr;
    }


       private static final String START = "12";
       private static final String END = "18";

       public static void main(String[] args) throws IOException {
          countLines("hsa00072.txt"); 
          Graph.Edge[] GRAPH=readTextFile("hsa00072.txt");
          Graph g = new Graph(GRAPH);
          g.dijkstra(START);
          g.printPath(END);


          //g.printAllPaths();
       }
    }

    class Graph {
       private final Map<String, Vertex> graph; // mapping of vertex names to Vertex objects, built from a set of Edges

       /** One edge of the graph (only used by Graph constructor) */
       public static class Edge {
          public final String v1, v2;
          public final int dist;
          public Edge(String v1, String v2, int dist) {
             this.v1 = v1;
             this.v2 = v2;
             this.dist = dist;
          }
       }

       /** One vertex of the graph, complete with mappings to neighbouring vertices */
       public static class Vertex implements Comparable<Vertex> {
          public final String name;
          public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity
          public Vertex previous = null;
          public final Map<Vertex, Integer> neighbours = new HashMap<>();

          public Vertex(String name) {
             this.name = name;
          }

          private void printPath() {
             if (this == this.previous) {
                System.out.printf("%s", this.name);
             } else if (this.previous == null) {
                System.out.printf("%s(unreached)", this.name);
             } else {
                this.previous.printPath();
                System.out.printf(" -> %s(%d)", this.name, this.dist);
             }
          }

          public int compareTo(Vertex other) {
             return Integer.compare(dist, other.dist);
          }
       }

       /** Builds a graph from a set of edges */
       public Graph(Edge[] edges) {
          graph = new HashMap<>(edges.length);

          //one pass to find all vertices
          for (Edge e : edges) {
             if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1));
             if (!graph.containsKey(e.v2)) graph.put(e.v2, new Vertex(e.v2));
          }

          //another pass to set neighbouring vertices
          for (Edge e : edges) {
             graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist);
             //graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also do this for an undirected graph
          }
       }

       /** Runs dijkstra using a specified source vertex */ 
       public void dijkstra(String startName) {
          if (!graph.containsKey(startName)) {
             System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName);
             return;
          }
          final Vertex source = graph.get(startName);
          NavigableSet<Vertex> q = new TreeSet<>();

          // set-up vertices
          for (Vertex v : graph.values()) {
             v.previous = v == source ? source : null;
             v.dist = v == source ? 0 : Integer.MAX_VALUE;
             q.add(v);
          }

          dijkstra(q);
       }

       /** Implementation of dijkstra's algorithm using a binary heap. */
       private void dijkstra(final NavigableSet<Vertex> q) {      
          Vertex u, v;
          while (!q.isEmpty()) {

             u = q.pollFirst(); // vertex with shortest distance (first iteration will return source)
             if (u.dist == Integer.MAX_VALUE) break; // we can ignore u (and any other remaining vertices) since they are unreachable

             //look at distances to each neighbour
             for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
                v = a.getKey(); //the neighbour in this iteration

                final int alternateDist = u.dist + a.getValue();
                if (alternateDist < v.dist) { // shorter path to neighbour found
                   q.remove(v);
                   v.dist = alternateDist;
                   v.previous = u;
                   q.add(v);
                } 
             }
          }
       }

       /** Prints a path from the source to the specified vertex */
       public void printPath(String endName) {
          if (!graph.containsKey(endName)) {
             System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName);
             return;
          }

          graph.get(endName).printPath();
          System.out.println();
       }
       /** Prints the path from the source to every vertex (output order is not guaranteed) */
       public void printAllPaths() {
          for (Vertex v : graph.values()) {
             v.printPath();
             System.out.println();
          }
       }

    }
12  ECrel   15
15  ECrel   18
11  ECrel   12
12  ECrel   14
11  ECrel   14
11  ECrel   18
14  maplink 17
这些函数和main在一个叫做Dijkstra的类中。整个代码如下:

 private static final Graph.Edge[] Arr = {
          new Graph.Edge("a", "b", 1),
          new Graph.Edge("a", "c", 1),
          new Graph.Edge("a", "f", 1),
          new Graph.Edge("b", "c", 1),
          new Graph.Edge("b", "d", 1),
          new Graph.Edge("c", "d", 1),
          new Graph.Edge("c", "f", 1),
          new Graph.Edge("d", "e", 1),
          new Graph.Edge("e", "f", 1),
       };
public static Graph.Edge[] readTextFile(String fileName) {

    String line = null;
    Graph.Edge[] Gr=new Graph.Edge[Dijkstra.count-1];
    try {
        FileReader fileReader = new FileReader("txt2.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        int i=0;
        while ((line = bufferedReader.readLine()) != null) {
            String[] tokens = line.split("\\t+");
            String s = tokens[0];
            String e = tokens[2];
            Gr[i] =new Graph.Edge(s, e, 1);
            i=i+1;
        }

        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");
    } catch (IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");
    }
    return Gr;
    }
package shortestPath;
import java.util.Scanner;
import java.io.*;
import java.util.*;

public class Dijkstra {

/*private static final Graph.Edge[] Arr = {
          new Graph.Edge("a", "b", 1),
          new Graph.Edge("a", "c", 1),
          new Graph.Edge("a", "f", 1),
          new Graph.Edge("b", "c", 1),
          new Graph.Edge("b", "d", 1),
          new Graph.Edge("c", "d", 1),
          new Graph.Edge("c", "f", 1),
          new Graph.Edge("d", "e", 1),
          new Graph.Edge("e", "f", 1),
    };*/
    public static int count;
    //public static Graph.Edge[] GRAPH = new Graph.Edge[count] ;

    public static void countLines(String file) throws IOException
    {
    LineNumberReader  lnr = new LineNumberReader(new FileReader(new File(file)));
    lnr.skip(Long.MAX_VALUE);
    Dijkstra.count=lnr.getLineNumber() + 1; //Add 1 because line index starts at 0
    // Finally, the LineNumberReader object should be closed to prevent resource leak
    lnr.close();
    //return Dijkstra.count;
    }

    public static Graph.Edge[] readTextFile(String fileName) {

    String line = null;
    Graph.Edge[] Gr=new Graph.Edge[Dijkstra.count-1];
    try {
        FileReader fileReader = new FileReader("txt2.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        int i=0;
        while ((line = bufferedReader.readLine()) != null) {
            String[] tokens = line.split("\\t+");
            String s = tokens[0];
            String e = tokens[2];
            Gr[i] =new Graph.Edge(s, e, 1);
            i=i+1;
        }

        // Always close files.
        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");
    } catch (IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");
    }
    //return Dijkstra.GRAPH;
    return Gr;
    }


       private static final String START = "12";
       private static final String END = "18";

       public static void main(String[] args) throws IOException {
          countLines("hsa00072.txt"); 
          Graph.Edge[] GRAPH=readTextFile("hsa00072.txt");
          Graph g = new Graph(GRAPH);
          g.dijkstra(START);
          g.printPath(END);


          //g.printAllPaths();
       }
    }

    class Graph {
       private final Map<String, Vertex> graph; // mapping of vertex names to Vertex objects, built from a set of Edges

       /** One edge of the graph (only used by Graph constructor) */
       public static class Edge {
          public final String v1, v2;
          public final int dist;
          public Edge(String v1, String v2, int dist) {
             this.v1 = v1;
             this.v2 = v2;
             this.dist = dist;
          }
       }

       /** One vertex of the graph, complete with mappings to neighbouring vertices */
       public static class Vertex implements Comparable<Vertex> {
          public final String name;
          public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity
          public Vertex previous = null;
          public final Map<Vertex, Integer> neighbours = new HashMap<>();

          public Vertex(String name) {
             this.name = name;
          }

          private void printPath() {
             if (this == this.previous) {
                System.out.printf("%s", this.name);
             } else if (this.previous == null) {
                System.out.printf("%s(unreached)", this.name);
             } else {
                this.previous.printPath();
                System.out.printf(" -> %s(%d)", this.name, this.dist);
             }
          }

          public int compareTo(Vertex other) {
             return Integer.compare(dist, other.dist);
          }
       }

       /** Builds a graph from a set of edges */
       public Graph(Edge[] edges) {
          graph = new HashMap<>(edges.length);

          //one pass to find all vertices
          for (Edge e : edges) {
             if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1));
             if (!graph.containsKey(e.v2)) graph.put(e.v2, new Vertex(e.v2));
          }

          //another pass to set neighbouring vertices
          for (Edge e : edges) {
             graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist);
             //graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also do this for an undirected graph
          }
       }

       /** Runs dijkstra using a specified source vertex */ 
       public void dijkstra(String startName) {
          if (!graph.containsKey(startName)) {
             System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName);
             return;
          }
          final Vertex source = graph.get(startName);
          NavigableSet<Vertex> q = new TreeSet<>();

          // set-up vertices
          for (Vertex v : graph.values()) {
             v.previous = v == source ? source : null;
             v.dist = v == source ? 0 : Integer.MAX_VALUE;
             q.add(v);
          }

          dijkstra(q);
       }

       /** Implementation of dijkstra's algorithm using a binary heap. */
       private void dijkstra(final NavigableSet<Vertex> q) {      
          Vertex u, v;
          while (!q.isEmpty()) {

             u = q.pollFirst(); // vertex with shortest distance (first iteration will return source)
             if (u.dist == Integer.MAX_VALUE) break; // we can ignore u (and any other remaining vertices) since they are unreachable

             //look at distances to each neighbour
             for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
                v = a.getKey(); //the neighbour in this iteration

                final int alternateDist = u.dist + a.getValue();
                if (alternateDist < v.dist) { // shorter path to neighbour found
                   q.remove(v);
                   v.dist = alternateDist;
                   v.previous = u;
                   q.add(v);
                } 
             }
          }
       }

       /** Prints a path from the source to the specified vertex */
       public void printPath(String endName) {
          if (!graph.containsKey(endName)) {
             System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName);
             return;
          }

          graph.get(endName).printPath();
          System.out.println();
       }
       /** Prints the path from the source to every vertex (output order is not guaranteed) */
       public void printAllPaths() {
          for (Vertex v : graph.values()) {
             v.printPath();
             System.out.println();
          }
       }

    }
12  ECrel   15
15  ECrel   18
11  ECrel   12
12  ECrel   14
11  ECrel   14
11  ECrel   18
14  maplink 17

问题是,每当我想找到从节点12到节点18的路径时,即使有路径,它也会说18(未实现),但它不会返回路径。对于程序中的输入,它工作正常并返回路径。问题只在尝试读取文本文件时出现。

这里发生了一些非常奇怪的事情。我一边工作一边唠叨

我随机得到不同的结果(有时你的程序按你想要的那样工作,有时不按你想要的那样工作)。通常,它们是不同的,这取决于我是否在调试模式下运行

所以。现在是寻找随机性来源的时候了。显然,你没有直接/有意地使用随机性。因此,我们应该在其他地方寻找随机性

我想我看到了随机性的两个潜在来源

  • 当您遍历NavigableSet时,它首先要做大量的相等值比较。这是必要的,也是有道理的,但它可能会导致随机性,因为它不必选择一个特定的顺序来向您呈现元素

  • 在算法中也使用映射,并且没有关于它们的入口集返回的顺序的承诺

  • 我猜你的算法实际上对事物呈现的顺序很敏感。我还没有深入了解哪些部分可能是问题所在,但如果两条同样好的路线以不同的顺序呈现给它,那么这一部分可能会走向两个不同的方向:

                if (alternateDist < v.dist) { // shorter path to neighbour found
                    q.remove(v);
                    v.dist = alternateDist;
                    v.previous = u;
                    q.add(v);
                }
    
    if(alternateDist
    可能这些路线中有一条永远不会到达你要寻找的目的地

    实际上,我建议拿出一些纸片,在上面写上顶点,并在算法需要对映射入口集进行迭代器时对它们进行洗牌

    我怀疑为什么在从源代码读取时这种方法有效,而在从代码读取时失败:不同的随机种子。就这样。从技术上讲,这两种情况下都容易出现相同的故障,只是碰巧在一个JVM场景中选择了一条工作路径,在另一个JVM场景中选择了一条非工作路径


    抱歉,这有点模糊而且很长。也许会有帮助

    代码片段用于可由HTML/JS/CSS等浏览器运行的代码。对于Java,使用
    code-Sample
    编辑器上的哪个图标看起来像
    {}
    。您是否尝试将
    Arr
    GRAPH
    进行比较,看看它们是否不同?你得到的数组值相同吗?Vivin Paliath我只有GRAPH或Arr。它们是相同的,但我在程序中输入时使用Arr,我不会调用readTextFile函数,因为它将从文本文件读取。非常感谢。我想我可能对我编写的函数有一些问题,或者对这些函数的调用或传递参数有一些问题。再次感谢你。