0

Let me start by saying I have a file. For more info, you check it here.

I have already added the edges on the hashmap with multiple values. I have check it by adding this piece of code:

        map.put(nodes.get(i), edges);
        System.out.println(nodes.get(i) + " " +  map.get(nodes.get(i)));

After adding each entry to map, I check it right away if it was successfully added. Yes, it worked.

Here's the output:

a0 [acodijkstra.Edge@b1c5fa]
a1 [acodijkstra.Edge@13caecd, acodijkstra.Edge@f84386, acodijkstra.Edge@1194a4e]
a2 [acodijkstra.Edge@15d56d5, acodijkstra.Edge@efd552, acodijkstra.Edge@19dfbff]
a3 [acodijkstra.Edge@10b4b2f, acodijkstra.Edge@750159, acodijkstra.Edge@1abab88]

But when creating another method to display the content of the map, I found out that the map is empty. Here's the code:

public void viewFile() {
    for(int i=0; i<nodes.size();i++) {
        System.out.println(nodes.get(i) + " " + this.map.get(nodes.get(i)));
    }
}

The output of the above code is this:

a0 []
a1 []
a2 []
a3 []

What could be the possible reason for this? I am really confused why this happened.

For the code, here's the simplified version (if complied, this will result to errors since I edited some parts deemed unnecessary out):

class Reader {

    HashMap<String, Vertex> vertexList;
    Map<String, ArrayList<Edge>> map;
    ArrayList<String> nodes;
    ArrayList<Edge> edges;


public Reader(String fileName) {
    vertexList = new HashMap<String, Vertex>();
    map = new HashMap<String, ArrayList<Edge>>();
    nodes = new ArrayList<String>();
    edges = new ArrayList<Edge>();
    readFile(fileName);
}

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();
        setVertices();
        System.out.println();
        file.close();
    } catch(Exception e){
        System.out.println(e);
    }
}
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());
        adjLine.add(line.substring(line.indexOf("-")+1).trim());
    }
}

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);
        System.out.println(nodes.get(i) + " " +  map.get(nodes.get(i)));
        edges.clear();
    }
}
6
  • We need to see more of your code, in particular the code that builds the map. Commented Mar 19, 2013 at 6:21
  • @princepiero how and where do you call the viewFile() method? Commented Mar 19, 2013 at 6:25
  • @Lakshmi included above. Commented Mar 19, 2013 at 6:29
  • @princepiero i still dont find the vieFile method is it in seperate class? seems like tat to me . Plus your variables are all instance variables and not class variables so for each object that you make of this class the values will be different. Commented Mar 19, 2013 at 6:34
  • 1
    k fine then what @NPE said is right. Commented Mar 19, 2013 at 6:39

1 Answer 1

2

Here:

    map.put(nodes.get(i), edges);
    System.out.println(nodes.get(i) + " " +  map.get(nodes.get(i)));
    edges.clear();

you're storing a reference to edges in the map, and then clearing edges. This is why, while the elements are still in the map, they are blank.

You need to stop reusing the same edges object, and create a new one for every vertex (why is edges even a member of your class, and not a local variable?)

Sign up to request clarification or add additional context in comments.

3 Comments

I tried deleting the edges.clear(); line then I got a lot of edges on the next elements to be added. The reason for adding .clear is to clear the ArrayList edges. Does it really matter since I have already added the edges on the map, @NPE?
@princepiero: You are not adding a copy of edges, you are adding a reference to it. If, after adding it to the map, you change edges, the map changes.
This solved the problem. Thank you. I just made the edges a local variable and initialized it inside the loop. Great help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.