简体   繁体   中英

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;
private 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();
}

如果要在打印结果时有更好的描述,请重写Person类中的toString()方法。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM