java - 返回对象值

发布于 2024-10-09 07:34:22 字数 923 浏览 3 评论 0原文

您好,我有以下代码:

public List<Person> findAll() {

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

    return copy;

}

但是当我测试这个时,我只检索以下内容而不是值:

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

我如何获取值而不是像上面那样。在我插入人员的地方,代码如下所示:

public boolean insert(String name, String nbr) {

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

            return true;
}

这是我的 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;
}
}

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;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雨的味道风的声音 2024-10-16 07:34:22

您已经收到了您想要的物品。

您看到的是这些对象的内部表示。

您必须迭代它们并调用它们各自的方法才能查看您可能想要查看的信息。

如果您对这些结果不满意,则必须重写 toString 才能为您提供更有意义的信息。

更新

看到您的编辑后,您应该在您的 Person 类中添加与此类似的 toString

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

顺便说一下,您正在存储 < code>nbr 作为一个字符串,很明显它应该是一个整数。因此,我建议将其类型更改为 intInteger

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.

懵少女 2024-10-16 07:34:22

您将返回一个 List 对象。您可以使用 Person 对象来获取所需的数据。要获取 Person 对象,请迭代列表。

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

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();
}
浮生面具三千个 2024-10-16 07:34:22

如果您希望在打印结果时获得更好的描述,请重写 Person 类中的 toString() 方法。

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

猥琐帝 2024-10-16 07:34:22

在类 Person 中放入类似的内容(不要更改方法名称!):

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

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

public String toString() {
    return name;//change this line
}
ま昔日黯然 2024-10-16 07:34:22

您正在打印一个具有从 Object 类继承的默认 toString 的对象。这将打印出对象的类型及其在内存中的位置(即:Person@1ded0fd)。

如果您希望它看到其他内容,您可以重写类中的 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;
  }
}

如果您的类类似于上面的内容,这将允许您执行如下操作:

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

您也可以按原样获取它并打印出来无需重写 toString 方法即可从中获取任何您想要的信息。

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

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
戏舞 2024-10-16 07:34:22

您希望从 ArrayList 中检索 Person 的什么值或类属性?这种值(Person@15c7850等)表明,当您使用

System.out.print(copy)时,JVM分配的Person对象的随机id。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文