【发布时间】:2013-12-12 15:24:28
【问题描述】:
您好,我是新来的,我有一个问题。
我有这个对象。
public Person(int id,String name, int age){
this.id=id;
this.name=name;
this.age=age;
}
我想创建一个学生 ID 集合。
到现在为止
List<Person> students=new ArrayList<Person>();
Collection<Integer> studentsIds=new ArrayList<Person>(students);
有人可以帮我吗?
【问题讨论】:
-
为什么需要两组数据? Person 类包含所有 id。如果您需要一个 id 列表,只需遍历学生列表
-
您可能想查看 HashMap,它是 KeyValues 的集合。根据您使用此代码的目标,它可能更接近您想要实现的目标。
标签: java list object collections