开发者

java - return object value

Hi i have the following code:

public List<Person> findAll() {

    List<Person> copy = new ArrayList<Person>();
    for (Person person : personer) {
        copy.add(person);
    }

    return copy;

}

But when i test this i only retrieve the following and not the value:

[Person@15c7850, Person@1ded0fd, Person@16a9d42]

How do i get the values and not like above. Where i am inserting the person the code looks like this:

public boolean insert(String name, String nbr) {

    if (containsName(name)) {
                    return false;
            }
    Person person = new Person(name, nbr);
    personer.add(person);

            return true;
}

and here is my Person class:

class Person {

private String name;
priva开发者_开发技巧te String nbr;





public Person (String name, String nbr) {
    this.name = name;
    this.nbr = nbr;
}


public String getName() {
    return name;
}


public String getNumber() {
    return nbr;
}
}


You're already receiving the objects you want.

What you see is an internal representation of these objects.

You must iterate through them and call their respective methods to see the information you probably want to see.

If you're not satisfied with these results, you must override toString to provide you with more meaningful information.

Update:

after seeing your edit, you should add toString similar to this one in your Person class:

@Override
public String toString() {
    return "Name: " + name + ", number: " + nbr; 
}

By the way, you're storing nbr as a string, and it's obvious it should be an integer. So, I'd suggest changing its type to an int or Integer.


You are getting a List object back. You can use the Person object to get the data that you need. To get to the Person objects, iterate over the list.

List<Person> people = findAll();
for Person p : people {
    String phoneNumber = p.phoneNumber();
    String name = p.Name();
}


Override the toString() method in the Person class if you want a better description when printing the results.


Put something like this in the class Person (don't change the method name!):

public String toString() {
    return name;//change this line
}


You are printing out an Object that has the default toString inherited from the Object class. This will print out the type of object it is and its location in memory (ie: Person@1ded0fd).

If you'd like it to see something else, you can override the toString method within your class:

public class Person {
  private String name;

  public Person(String name) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }    

  public String toString() {
    return this.name;
  }
}

If your class looked like the above, this would allow you to do something like this:

Person p = new Person("John");
System.out.println(p);
 > John

You can also just grab it as is and print out any information you want from it without overriding the toString method.

Person p = new Person("John");
System.out.println(p.getName());
 > John


What value or class Person's property you aspect to retrieve from the ArrayList? This kind of value(Person@15c7850, etc) shows that the Person's object random id that assigned by JVM when you use

System.out.print(copy).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜