嗨,我是新來的,我有一個問題。如何使用「人物」列表製作整數集合?
我有這個對象。
public Person(int id,String name, int age){
this.id=id;
this.name=name;
this.age=age;
}
而我想創建一個Collection studentsIds。
到現在爲止我已經
List<Person> students=new ArrayList<Person>();
Collection<Integer> studentsIds=new ArrayList<Person>(students);
有人能幫助我嗎?
爲什麼要使用兩組數據? Person類包含所有的id。如果你需要一個ID列表,只需遍歷學生列表 –
你可能想查看一個[HashMap](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap .html)這是一個KeyValues的集合。這可能更接近你想要實現的目標,取決於你使用這段代碼的目標。 – DoubleDouble