- Java code
import java.util.ArrayList;import java.util.Iterator;import java.util.List;class Student{ private Boolean sex; private int age; private String name; public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } 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 class listTest { /** * @param args */ public static void main(String[] args) { List al = new ArrayList(); Student std = new Student(); std.setAge(10); std.setName("zhangsan"); std.setSex(true); Student std1 = new Student(); std.setAge(20); std.setName("lishi"); std.setSex(true); Student std2 = new Student(); std.setAge(19); std.setName("wangwu"); std.setSex(true); al.add(std); al.add(std1); al.add(std2); for(Iterator ite = al.iterator();ite.hasNext();){ Student s = (Student)ite.next(); System.out.println(s.getAge()+"--"+s.getName()+"--"+s.getSex()); } }}
打印结果为:
19--wangwu--true
0--null--null
0--null--null
想问下各位大哥怎样才能全部打印出来,小弟是新手所以还请大家帮帮忙。。。
------解决方案--------------------------------------------------------
晕 你怎么把值全部设置给std了啊,下面两个对象都没设置值。std.setAge(20);
std.setName("lishi");
std.setSex(true);
的std改成std1.
std.setAge(19);
std.setName("wangwu");
std.setSex(true);
的std改成std2