简体   繁体   English

java-返回对象值

[英]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] [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: 这是我的Person类:

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. 如果您对这些结果不满意,则必须重写toString才能为您提供更多有意义的信息。

Update : 更新

after seeing your edit, you should add toString similar to this one in your Person class: 看到您的编辑后,您应该在Person类中添加类似于此的toString

@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. 顺便说一句,您将nbr存储为字符串,很明显它应该是整数。 So, I'd suggest changing its type to an int or Integer . 因此,我建议将其类型更改为intInteger

You are getting a List object back. 您将获得一个List对象。 You can use the Person object to get the data that you need. 您可以使用Person对象来获取所需的数据。 To get to the Person objects, iterate over the list. 要访问Person对象,请遍历列表。

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!): Person类中放入类似的内容(不要更改方法名称!):

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

You are printing out an Object that has the default toString inherited from the Object class. 您正在打印一个具有从Object类继承的默认toString的Object。 This will print out the type of object it is and its location in memory (ie: Person@1ded0fd). 这将打印出对象的类型及其在内存中的位置(即:Person @ 1ded0fd)。

If you'd like it to see something else, you can override the toString method within your class: 如果您希望看到其他内容,可以在类中重写toString方法:

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. 您也可以直接获取它,并从中打印出所需的任何信息,而不必覆盖toString方法。

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? 您要从ArrayList检索哪个值或类Person的属性? This kind of value(Person@15c7850, etc) shows that the Person's object random id that assigned by JVM when you use 这种值(Person @ 15c7850等)表明,当您使用JVM时分配的Person的对象随机ID

System.out.print(copy) . System.out.print(copy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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