java - 返回对象值
您好,我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您已经收到了您想要的物品。
您看到的是这些对象的内部表示。
您必须迭代它们并调用它们各自的方法才能查看您可能想要查看的信息。
如果您对这些结果不满意,则必须重写
toString
才能为您提供更有意义的信息。更新:
看到您的编辑后,您应该在您的
Person
类中添加与此类似的toString
:顺便说一下,您正在存储 < code>nbr 作为一个字符串,很明显它应该是一个整数。因此,我建议将其类型更改为
int
或Integer
。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 yourPerson
class: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 anint
orInteger
.您将返回一个 List 对象。您可以使用 Person 对象来获取所需的数据。要获取 Person 对象,请迭代列表。
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.
如果您希望在打印结果时获得更好的描述,请重写 Person 类中的 toString() 方法。
Override the toString() method in the Person class if you want a better description when printing the results.
在类
Person
中放入类似的内容(不要更改方法名称!):Put something like this in the class
Person
(don't change the method name!):您正在打印一个具有从 Object 类继承的默认 toString 的对象。这将打印出对象的类型及其在内存中的位置(即:Person@1ded0fd)。
如果您希望它看到其他内容,您可以重写类中的 toString 方法:
如果您的类类似于上面的内容,这将允许您执行如下操作:
您也可以按原样获取它并打印出来无需重写 toString 方法即可从中获取任何您想要的信息。
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:
If your class looked like the above, this would allow you to do something like this:
You can also just grab it as is and print out any information you want from it without overriding the toString method.
您希望从 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)
.