views:

98

answers:

4

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));
    }
A: 

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!

suicideducky
Yes I've got getter and setter.
Max
A: 

If you asking such questions then you didn't understand some basics of programming. Plz read some tutorial on Java or a good book.

kalkin
Unfortunately dont have a time to do tutorial or read a good book at the moment.Thanks for your suggestion any way
Max
If you do not have time to read a book or do a tutorial then you do not have time to do anything related to programming and really should have your keyboard removed from you at gunpoint if necessary.
JUST MY correct OPINION
Will try to. Thank you
Max
+1  A: 

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()).

Eyal Schneider
A: 

Need more details on ListOfppl class because it's obviously not a library included with Java

Bo Tian