开发者

How do I add an object to a binary tree based on the value of a member variable?

How can I get a specific value from an object? I'm trying to get a value of an instance for eg.

ListOfPpl newListOfPpl = new ListOfPpl(id, name, age);
Object item = newListOfPpl;

How can I get a value of name from an Object item?? Even if it is easy or does not interest you can anyone help me??

Edited: I was trying to build a binary tree contains the node of ListOfPpl, and need to sort it in the lexicographic. Here's my code for insertion on the node. Any clue??

    public void insert(Object item){
    Node current = root;
    Node follow = null;
    if(!isEmpty()){
        root = new Node(item, null, null);
        return;
    }boolean left = false, right = false;
    while(current != null){
    follow = current;
        left = false;
        right = false;
                      //I need to compare and sort it
             if(item.compareTo(current.getFighter()) < 0){
                                 current = current.getLeft();
            left = true;
        }else {
        current = current.getRight();
            right = true;
        }
   开发者_运维问答     }if(left)
            follow.setLeft(new Node(item, null, null));
        else
            follow.setRight(new Node(item, null, null));
    }


Since your item variable is declared to be of the most basic Java type Object, you can't extract anything related to your data from it directly. The variable newListOfPpl, on the other hand, which is a reference to the same object, is declared to be of type ListOfPpl, so you can invoke on it whatever getter methods that have been defined in it (possibly getId(), getName(), getAge()).


That all depends on what ListOfPpl 'has inside it'. Does it have a method to access it's data? or public data members?

For example, if ListOfPpl had a .getname() method. Without knowing more about ListOfPpl I don't think I can be off much more help sorry.

Hope this was useful to you, and goodluck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜