【问题标题】:To display collection content [duplicate]显示集合内容[重复]
【发布时间】:2016-05-02 02:44:14
【问题描述】:

我在 arraylist 中存储了 13 个对象,但如果打印 arraylist,那么它显示数组的地址为 [com.psl.beans.Student@e83912, com.psl.beans.Student@1fae3c6,com.psl.beans.Student@7ffe01, com.psl.beans.Student@fd13b5,com.psl.beans.Student@118f375 , com.psl.beans.Student@117a8bd,com.psl.beans.Student@471e30, com.psl.beans.Student@10ef90c,com.psl.beans.Student@a32b, com.psl.beans.Student@1d8957f ,com.psl.beans.Student@3ee284, com.psl.beans.Student@8965fb,com.psl.beans.Student@867e89]

但我想在打印列表时查看该列表中对象的内容,所以有什么办法可以做到这一点..

【问题讨论】:

  • toString()方法添加到com.psl.Student

标签: java spring arraylist collections


【解决方案1】:

重写 toString() 方法

示例

class Student {
    private String name;
    private String lastname;        

    @Override
    public String toString() {
        return this.name + " " + this.lastname;
    }
}

您应该对 Student 类的属性执行相同的操作,并在每次要显示其内容时调用 Student.toString() 方法

【讨论】:

  • 你怎么知道有变量namelastName
  • 所以编辑你的答案并在你的答案中明确说明这是一个例子,并解释为什么这会有所帮助
  • 代码块上方的'Example'这个词还不够?
  • 添加一些解释,说明为什么以及如何解决上述问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 2014-02-15
  • 1970-01-01
  • 2014-09-19
  • 1970-01-01
  • 1970-01-01
  • 2017-01-31
相关资源
最近更新 更多