Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2011
    Posts
    98
    Rep Power
    0

    Default What does (Node)nodes.get(j)) means here?

    Hello,

    Please find the following file attached 1) Node 2)Main 3) Graph

    BAsically I'm implementing BFS and DFS and have some questions regarding some code in Graph File:

    Java Code:
    private Node getUnvisitedChildNode(Node n)
    	{
    		
    		int index=nodes.indexOf(n);
    		int j=0;
    		while(j<size)
    		{
    			if(adjMatrix[index][j]==1 && ((Node)nodes.get(j)).visited==false)
    			{
    				return (Node)nodes.get(j);
    			}
    			j++;
    		}
    		return null;
    	}
    Could anyone tell me what does
    Java Code:
    (Node)nodes.get(j))
    means here?

    Thanks
    Attached Files Attached Files

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: What does (Node)nodes.get(j)) means here?

    Java Code:
    (Node) nodes.get(j);
    means is get an object at j index on the nodes ArrayList and cast it to Node object. If you use generic in your array list then you don't need to do object casting.

    Java Code:
    ArrayList<Node> nodes = new ArrayList<>();
    ...
    Node n = nodes.get(index);

  3. #3
    Join Date
    Nov 2011
    Posts
    98
    Rep Power
    0

    Default Re: What does (Node)nodes.get(j)) means here?

    Thanks for your answer. I have few more questions:
    In bfs() method in the Graph class, I have following Questions:

    Here is the bfs() method for your quick reference:

    Java Code:
    public void bfs()
    	{
    		
    		//BFS uses Queue data structure
    		Queue<Node> q=new LinkedList<Node>();
    		q.add(this.rootNode);
    		printNode(this.rootNode);
    		rootNode.visited=true;
    		while(!q.isEmpty())
    		{
    			Node n=q.remove();
    			Node child=null;
    			while((child=getUnvisitedChildNode(n))!=null)
    			{
    				child.visited=true;
    				printNode(child);
    				q.add(child);
    			}
    		}
    		//Clear visited property of nodes
    		clearNodes();
    	}


    1) As I understood that "Queue<Node> q=new LinkedList<Node>();" means LinkList implements a Queue.
    So, why LinkedList is required in this case, can't we just do it via "Queue<node> q = new Queue<node>();" ?


    2) Although I have read the usage of "this" keyword many times, could anyone please explain why

    q.add(this.rootNode) is used and why not just q.add(rootNode) ?

    3) What does the following line signifies ?

    Java Code:
    while((child=getUnvisitedChildNode(n))!=null)
    Quote Originally Posted by wsaryada View Post
    Java Code:
    (Node) nodes.get(j);
    means is get an object at j index on the nodes ArrayList and cast it to Node object. If you use generic in your array list then you don't need to do object casting.

    Java Code:
    ArrayList<Node> nodes = new ArrayList<>();
    ...
    Node n = nodes.get(index);

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: What does (Node)nodes.get(j)) means here?

    1. Because java.util.Queue is an interface, it's abstract so that you cannot create an instance of Queue.

    2. this give you a reference to the current object you are in. In your case you can either use the this keyword or omit it. But you can use it to emphasize that you the rootNode instance variable of this (your current) object.

    3. the statement while((child = getUnvisitedChildNode(n)) != null) can be read as call the getUnvisitedChildNode(n) method, store the result to child variable. And repeat the process while the result is not null.

  5. #5
    Join Date
    Nov 2011
    Posts
    98
    Rep Power
    0

    Default Re: What does (Node)nodes.get(j)) means here?

    Awesome! Thanks for answering my questions
    Quote Originally Posted by wsaryada View Post
    1. Because java.util.Queue is an interface, it's abstract so that you cannot create an instance of Queue.

    2. this give you a reference to the current object you are in. In your case you can either use the this keyword or omit it. But you can use it to emphasize that you the rootNode instance variable of this (your current) object.

    3. the statement while((child = getUnvisitedChildNode(n)) != null) can be read as call the getUnvisitedChildNode(n) method, store the result to child variable. And repeat the process while the result is not null.

  6. #6
    Join Date
    Nov 2011
    Posts
    98
    Rep Power
    0

    Default Re: What does (Node)nodes.get(j)) means here?

    One more question:

    Why utility method like clearNodes() is required here?

    Quote Originally Posted by Jack_Tauson_Sr View Post
    Awesome! Thanks for answering my questions

Similar Threads

  1. Replies: 11
    Last Post: 02-08-2012, 02:43 PM
  2. CheckBox node tree with two diferent kind of nodes
    By cdinesh1 in forum AWT / Swing
    Replies: 0
    Last Post: 02-03-2012, 07:02 AM
  3. Replies: 5
    Last Post: 12-21-2011, 10:45 PM
  4. Linking nodes/adding node data
    By falkon114 in forum New To Java
    Replies: 0
    Last Post: 04-19-2011, 09:44 PM
  5. Replies: 0
    Last Post: 11-29-2010, 01:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •