java顺序表打印出来的是地址_为什么arraylist打印出来的是地址

在Java中,当使用ArrayList存储自定义对象并尝试打印时,实际输出的是对象的内存地址。这是因为默认情况下,`toString()`方法返回的是对象的哈希码。要打印对象的内容,需要在`student`类中重写`toString()`方法,展示idString、genderString和age等属性。
摘要由CSDN通过智能技术生成

importjava.util.ArrayList;publicclasstest{publicstaticvoidmain(String[]args){ArrayListarrayList=newArrayList<>();arrayList.add(newstudent("teacher","male",18));a...

import java.util.ArrayList;

public class test {

public static void main(String[] args) {

ArrayList arrayList = new ArrayList<>();

arrayList.add(new student("teacher", "male", 18));

arrayList.add(new student("teacher", "famle", 45));

arrayList.add(new student("student", "female", 56));

arrayList.add(new student("teacher", "male", 21));

arrayList.add(new student("teacher", "female", 22));

arrayList.add(new student("student", "male", 34));

arrayList.add(new student("student", "male", 23));

arrayList.add(new student("student", "female", 32));

for(student s:arrayList) {

System.out.println(s);

}

}

}

class student{

String idString;

String genderString;

int age;

public student(String idString, String genderString, int age) {

super();

this.idString = idString;

this.genderString = genderString;

this.age = age;

}

}

展开

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值