?List集合中如果放入的是Object(),直接打印object对象的话,控制台输出的是首地址。
?
Object obj=new Object();
System.out.println(obj.toString());
?创建一个student类,包括年龄,姓名,性别,学号四个属性,并再TestStudent测试类中创建几个student类对象放入List集合中
Student类
public class Student { private int age; private String name; private String sex; private int id; public Student() { } public Student(int age, String name) { this.age = age; this.name = name; } public Student(int age, String name, String sex, int id) { this.age = age; this.name = name; this.sex = sex; this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
TestStudent测试
public class TestStudent { public static void main(String[] args) { Student stu1=new Student(18,"张三","男",101); Student stu2=new Student(15,"李四","男",102); Student stu3=new Student(25,"王五","男",103); Student stu4=new Student(56,"赵六","男",104); Student stu5=new Student(23,"张飞","男",105); List list=new ArrayList(); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); for (int i=0;i<list.size();i++){ System.out.println(list.get(i));//想要遍历List集合得到student对象的结果,必须重写toString方法,否则打印的是首地址 }System.out.println("------------==========="); for (int i=0;i<list.size();i++){ Object o = list.get(i); Student s=(Student)o; System.out.println(s);//默认调用toString方法 }System.out.println("---------"); Iterator it=list.iterator(); //迭代器遍历List集合 while (it.hasNext()){ Student s=(Student) it.next(); if (s.getAge()>=20){ System.out.println(s.toString()); } }} }
?如果用上面for循环遍历list集合或者迭代器,控制台打印的是集合每个元素的首地址
?
?可以通过student构造器来打印出list集合中的student对象
for (Object ob: list){ Student s=(Student) ob;//向下转型 ??????System.out.println(s.getName()+"\t"+s.getAge()+"\t"+s.getSex()+"\t"+s.getId()); }
?
?还有一个更简单的方法,就是在Student类中重写toString方法,这样不管用哪种方法遍历List集合,得到的都是student对象。
?
public class TestStudent { public static void main(String[] args) { Student stu1=new Student(18,"张三","男",101); Student stu2=new Student(15,"李四","男",102); Student stu3=new Student(25,"王五","男",103); Student stu4=new Student(56,"赵六","男",104); Student stu5=new Student(23,"张飞","男",105); List list=new ArrayList(); list.add(stu1); list.add(stu2); list.add(stu3); list.add(stu4); list.add(stu5); for (int i=0;i<list.size();i++){ System.out.println(list.get(i));//想要遍历student结果,必须重写toString方法 } System.out.println("------------==========="); for (int i=0;i<list.size();i++){ Object o = list.get(i); Student s=(Student)o; System.out.println(s);//默认调用toString方法 } for (Object ob: list){ Student s=(Student) ob;//向下转型 // System.out.println(s.getName()+"\t"+s.getAge()+"\t"+s.getSex()+"\t"+s.getId()); //没有重写toString方法,那么除了这一行,其他的都是打印的地址 System.out.println(s.toString()); } System.out.println("---------"); Iterator it=list.iterator(); while (it.hasNext()){ Student s=(Student) it.next(); if (s.getAge()>=20){ System.out.println(s.toString());//重写toString方法后,toString可以省略不写 } } } }
?
当List集合中放入类对象时,在类中重写toString方法就可以解决控制台打印出首地址的问题。
?