I am trying to create a bfs program but I am getting stuck with the Node class ArrayList retrieval part. I am trying to create 28 nodes, each with their own adjacency list. I want to retrieve the adjacency list from any given node so i can perform operations based on the node mapped to the string in the adjacency list. When I try to retrieve the adjacency list, it comes back empty when there should be, for example in node 1, a list with the string 2, 4, 5. this way i can get the node mapped to the string key "2", etc.The code is:
class Node{ String name; ArrayList<String> list; public Node(){} public Node( String name, ArrayList<String> list){ this.name = name; this.list = list; for( String n : list ){ System.out.println("DEBUG: test if list got passed in " + n ); }//it does print the list elements } } public static void BFS(){ //Queue<Node> q = new LinkedList<Node> (); Vector<Node> q = new Vector<Node> (); Map<String, Node> allNodes = new HashMap<String, Node> (28); ArrayList<String> l = new ArrayList<String>(); l.add("2"); l.add("4"); l.add("5"); Node One = new Node("1", l); allNodes.put("1", One ); // clear l so it can contain new elements to be sent to Node constructor l.clear(); l.add("3"); l.add("6"); Node Two = new Node("2", l); allNodes.put("2", Two); l.clear() Node j = allNodes.get("1"); System.out.println("J = 1---- J's name = " +j.name); System.out.println("J's list = " +j.list); ....}
The output i get is:
DEBUG: test if list got passed in 2
DEBUG: test if list got passed in 4
DEBUG: test if list got passed in 5
J = 1---- J's name = 1
J's list = []
the list is empty. I want it to retrieve the nodes adjacency list. How do I fix this?
Thanks in advance
I also attached the full code file for people but its not needed for my question i believe.