Java HashMap File Input Version of Code

偶尔善良 提交于 2019-12-25 18:40:16

问题


I have here a piece of code. This is a snippet from my Dijkstra implementation. Here, we have 4 vertices instantiated and each vertex has specified edges and its distance from the vertex(node). This code is straightforward and beneficial if the data will directly be inputted in the main class.

Vertex v0 = new Vertex("Redvile");
Vertex v1 = new Vertex("Blueville");
Vertex v2 = new Vertex("Greenville");
Vertex v3 = new Vertex("Orangeville");
Vertex v4 = new Vertex("Purpleville");

v0.adjacencies = new Edge[]{ new Edge(v1, 5),
                             new Edge(v2, 10),
                                 new Edge(v3, 8) };
v1.adjacencies = new Edge[]{ new Edge(v0, 5),
                             new Edge(v2, 3),
                             new Edge(v4, 7) };
v2.adjacencies = new Edge[]{ new Edge(v0, 10),
                                 new Edge(v1, 3) };
v3.adjacencies = new Edge[]{ new Edge(v0, 8),
                             new Edge(v4, 2) };
v4.adjacencies = new Edge[]{ new Edge(v1, 7),
                                 new Edge(v3, 2) };

However, my data is from a file. I have to read the file and create instances of the vertex together with the edges (adjacencies) on each vertex. I have a problem implementing this function. To wit, I need a code that translates this piece of code above to something wherein the data is from a file.

Here's my sample data:

a0 -    a1,1.6
a1 -    a0,1.6      * a2,0.85   * a3,0.5
a2 -    a1,0.85     * a34,1.2   * a39,0.65
a3 -    a1,0.5      * a4,0.2    * a5,0.1
a4 -    a3,0.2      * a5,0.2
a5 -    a4,0.1      * a6,1      * a36,0.65
a6 -    a5,1        * a7,1.5    * a14,0.45
a7 -    a6,1.5      * a8,0.18   * a11,1.2
a8 -    a7,0.18     * a9,1
a9 -    a8,1
a10 -   a13,1.9

The a0-a10 are the vertices and the a1,1.6 are others represent the adjacencies or edges connected to the vertex in (vertex,distance) format.

So far, I am able to read this file and put each line on an ArrayList. My problem now is basically how to instantiate each of the vertex and adding the adjacencies.

This is how I read the file:

private void readFile(String fileName) {

    try{
        FileReader file = new FileReader(fileName);
        Scanner sc = new Scanner(file);

        int i = 0;
        while (sc.hasNextLine()) {
          input.add(sc.nextLine());
          i++;
        }

        setNodes();
        System.out.println();
        file.close();
    } catch(Exception e){
        System.out.println(e);
    }
}

The input is the arrayList containing all the content of the file per line. On the other hand, nodes will have the list of all the vertices.

public void setNodes() {

    System.out.println();

    for(int i=0; i<input.size(); i++) {
        line = this.input.get(i);
        nodes.add(line.substring(0,line.indexOf("-")).trim());
    }
}

Also,

PS I also a problem with data type. My arrayList is string and the vertices are of type Vertex, which is a defined class in my code.

Should this description be inadequate, feel free to comment. Thank you and cheers!


回答1:


This should be straight forward

a0 -    a1,1.6
a1 -    a0,1.6      * a2,0.85   * a3,0.5

I will show you the process not the code

Create vertex_list list 
For each line read
  split on '-' left side is your current_vertex, right side is your 'adjacency list'
   check if your vertex is present in vertex_list if not create it and add it
   adj list =  a0,1.6      * a2,0.85   * a3,0.5
   split your adj list   on '*' 
   for each entry ex 'a0,1.6'
      split on ','  now you know the edge_vertex=split[0] = a0 and weight = split[1] = 1.6
      check if you already have edge_vertex in your vertex list if not add it
      create and add edge(edge_vertex, weight)  to current vertex
    end
 end

At the end of you process you should be able to display all your vertices from your vertex_list

As for giving a vertex a name you could do this in few different ways, easy way would be to give a vertex a name in the config file. This would required you to do two passes over you list, first one to generate each vertex with a name and second just like the pseudocode above except there is no need to create new vertexes as they have been created in first pass, you would simply add the edge vertex to the adj. list.

a0,Name A -    a1,1.6
a1,Name B -    a0,1.6      * a2,0.85   * a3,0.5
a2,Name C -    a1,0.85     * a34,1.2   * a39,0.65
a3,Name D -    a1,0.5      * a4,0.2    * a5,0.1



回答2:


I am publishing now an answer to this question based on my implementation and information given by @greg. Big thanks to him!

private void setVertices() {

    String[] listEdges;

    for(int i=0; i<nodes.size(); i++) {

        //if vertex does not exist, create it
        if(vertexList.containsKey(nodes.get(i))) {
            vertexList.put(nodes.get(i), new Vertex(nodes.get(i)));
        }

        line = adjLine.get(i);

        //separate adj edges to *
        if(line.contains("*")) {
            listEdges = line.split("[*]");
        } else {
            listEdges = new String[1];
            listEdges[0] = line;
        }

        //add edges to specified vertex
        for(int j=0; j < listEdges.length; j++ ) {
            String[] word = listEdges[j].split(",");
            edges.add(new Edge(vertexList.get(word[0]),Double.parseDouble(word[1])));
        }

        map.put(nodes.get(i), edges);
        edges.clear();
    }
}

Basically, I used ArrayList, Map and HashMap to be able to do this. Yes!



来源:https://stackoverflow.com/questions/15415722/java-hashmap-file-input-version-of-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!