(十五)java集合的小例子--集合存取使用对象
情景:定义一个学生类,和一个地址类,学生类中包含地址信息,其中一个学生可以有多个地址,将学生信息放入一个集合中,并将他们打印出来。
定义一个地址类:
public class Address {
private int aid;
private String phone;
private String address;
private int uid;
public Address(int aid, String phone, String address, int uid) {
super();
this.aid = aid;
this.phone = phone;
this.address = address;
this.uid = uid;
}
public Address() {
super();
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
@Override
public String toString() {
return "Address [aid=" + aid + ", phone=" + phone + ", address=" + address + ", uid=" + uid + "]";
}
}
定义一个学生类:注意一个学生可以有多个地址?这个怎么解决呢?如果只将地址类型设为Address,那么只能存在一个地址。如果用集合呢,可以存放多个地址吗?List< Address > address 这样就行了。那么student的构造函数就得改写了。
package 学生地址类;
import java.util.Date;
import java.util.List;
/**
* @author 刘平远
* @version 创建时间:2019年8月18日下午6:41:49
* 类说明 :
*/
public class Student {
private int id;
private String name;
private String sex;
private Date birth;
private List<Address> address;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int id, String name, String sex, Date birth) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.birth = birth;
}
public List<Address> getAddress() {
return address;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", birth=" + birth + ", address=" + address.toString()
+ "]";
}
public void setAddress(List<Address> address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
新建一个测试类:注意如何将地址信息加入学生信息中!
public class Test {
public static void main(String[] args) {
//新建3个学生对象
Student s1=new Student(1002,"liuqiang1","男",new Date());
Student s2=new Student(1002,"liuqiang2","男",new Date());
Student s3=new Student(1002,"liuqiang3","男",new Date());
//设置地址,
//将a1,a2地址传给s1
Address a1=new Address(1, "1001", "nanjin", 1001);
Address a2=new Address(1, "1001", "nanjin", 1001);
//将a3地址传给s2
Address a3=new Address(1, "1001", "nanjin", 1001);
//将a4地址传给s3
Address a4=new Address(1, "1001", "nanjin", 1001);
//s1.setAddress(a1);我定义的数据类型是List,只能传list类型的数据。
//定义一个类型为address的list集合
List<Address> s1addresses=new ArrayList<Address>();
//将地址添加到集合中
s1addresses.add(a1);
s1addresses.add(a2);
//将地址集合 添加到学生信息中
s1.setAddress(s1addresses);
//下面s2, s3的地址田间同理
List<Address> s2Addresses=new ArrayList<>();
s2Addresses.add(a3);
s2.setAddress(s2Addresses);
List<Address> s3Addresses=new ArrayList<>();
s3Addresses.add(a4);
s3.setAddress(s3Addresses);
//以上将学生地址信息添加完毕
//将学生信息放入 一个班级集合中 , 与将地址放入地址集合中的道理相同
List<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);
//遍历集合
Iterator<Student> iterator=list.iterator();
while (iterator.hasNext()) {
Student student = (Student) iterator.next();
System.out.println(student);
}
//也可以使用增强for循环
/* for (Student student : list) {
System.out.println(student);
}*/
}
}
,