tags:

views:

167

answers:

3

Hello,

I have the following classes:

public  class Model {
    protected String name;
    protected ArrayList<Model> children;

    public Model(){
        children = new ArrayList<Model>();
    }

    public void addChild(Model node) {
        children.add(node);       
    }
    //other code here
}


public class cPackage extends Model{
    public cPackage() {
        super();
    }

    //other code here
}


public class cClass extends Model{
    public cClass() {
        super();
    }
    //other code here
}

public class cMethod extends Model{
    public cMethod() {
        super();
    }
    //other code here
}

public void tryme(Object[] objs){
    String values = (String)objs[0];
    Model root = null;

    astNode = ax.getDeclaringNodeInProject(handles, bindings);

    if(astNode instanceof TypeDeclaration){
        TypeDeclaration  cl = (TypeDeclaration)astNode;
        root = new cClass();
        root.setName(cl.getName().getFullyQualifiedName());
    }else if(astNode instanceof MethodDeclaration){
        MethodDeclaration me = (MethodDeclaration)astNode;
        root = new cMethod();
        List mod =  me.parameters();
        String vars ="";
        ...
        root.setName(me.getName().getFullyQualifiedName() + "(" + vars + ")");
    }

    Model currentNode = null;
    Model prevNode = root;

    for(int i = 1 ; i < objs.length; i++){
        if(astNode instanceof PackageDeclaration){
            PackageDeclaration pka = (PackageDeclaration)astNode;
            p8.setName(pka.getFullyQualifiedName());
            if(currentNode == null) {
                currentNode = p8;
                prevNode.addChild(currentNode);
            }
            prevNode = currentNode;
        }else if(astNode instanceof MethodDeclaration){
            MethodDeclaration me = (MethodDeclaration)astNode;
            ...
            m8.setName(me.getName().getFullyQualifiedName() + "(" + vars + ")");

            if(currentNode == null) {
                currentNode = m8;
                prevNode.addChild(currentNode);
            }
            prevNode = currentNode;
        }else if(astNode instanceof TypeDeclaration){
            TypeDeclaration  cl = (TypeDeclaration)astNode;
            c8.setName(cl.getName().getFullyQualifiedName());
            if(currentNode == null) {
                currentNode = c8;
                prevNode.addChild(currentNode);
            }
            prevNode = currentNode;
        }
    }
}

Sorry if there are missing "{" or "}". What i am trying to do here is i have a data from the server like these:

[School Chair  Table Chalk]

...

[Apple Peer Mango Melon]

all in arrays. I want to build something like a tree-like structure whereby reading from the left :

Each element will be de parent of the element that follows it. I am looping through the data to do it. For example i want to get something like:

+School
      Chair
          Table
              Chalk


+Apple
     Peer
         Mango
             Melon

But when i run the above code i get only the first two elements like:

+School
      Chair



+Apple
     Peer

and the following elements don't show up.

Can someone help me where am doing wrong.

Thanks.

A: 

In the loop you currently do this:

  1. current node = next object from list
  2. add current node to previous node
  3. set previous node to current node
  4. Loop

What you end up with is a linked list. The root will only have one child in the children list. That child will have one child in it's children list and so on. To print it you have to iterate through each node in the linked list.

D. Wroblewski
+1  A: 

It looks like addChild() is only called when currentNode is null, and currentNode is only going to be null the first time through the loop. Thus only one child is ever added.

David Moles
A: 

I can well remember teh learning curve when first dealing with various data structures. (Pointer Stew anyone?)

One technique I found really helpful was to draw a diagram of what's going on.

 { name = "granddad",  children = --- }
                              |
  [  { name = "son1" , children = --- }  { name = "dottir1", children = --- } ... }
                                         |                                     |
                                 [ { ... } ... ]                 [ { ... }, { ...} ... ]

Then as you examine your code actual trace with your finger where you are in the diagram.

djna