嗨,我是新来的,我有一个问题。
我有这个对象。
public Person(int id,String name, int age){
this.id=id;
this.name=name;
this.age=age;
}
我想创建一个集合 studentIds。
到现在为止
List<Person> students=new ArrayList<Person>();
Collection<Integer> studentsIds=new ArrayList<Person>(students);
有人可以帮助我吗?
最佳答案
你不能那样做。两者是不同的数据类型。因此,创建一个 Integer 集合,然后查看 persons 集合。
List<Integer> studentsIds=new ArrayList<Integer>();
然后
for (Person p : students){
studentsIds.add(p.age); // change to p.id if you need
}
关于java - 如何使用 "Person"列表创建整数集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20547401/