Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Retrieving BFS code's adjacency list

  1. #1
    Junior Member
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Retrieving BFS code's adjacency list

    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.
    Attached Files Attached Files


Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. Which collection is best to do mathematical operation on it?
    By Sterzerkmode in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2009, 04:48 AM
  3. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 11:48 AM
  4. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM
  5. Replies: 1
    Last Post: November 22nd, 2008, 01:32 PM