你使用ArrayList
做什麼?您只需在學生課程中添加ArrayList<Course> courses
即可。
例:
public class Student{
public ArrayList<Course> courses = new Arraylist<Course>;
//Other stuff
public void addCourse(Course c){
courses.add(c);
}
}
編輯:
因爲這不會對OP的工作,我也包括了問題的約束範圍內的作品的方法。步驟:
- 找到的
Student
- 索引查找下一個
Student
- 廣場的指數新
Course
前右側的下一個Student
如果你的ArrayList被稱爲list
,那麼類似這樣的工作:
public void addCourse(Student student, Course course){
int studentIndex = -1;
int nextStudentIndex = -1;
for(int i=0; i<list.size(); i++){
if(!(list.get(i) instanceof Student)){
continue;
}
if(list.get(i).equals(student)){
studentIndex = i;
continue;
}
if(studentIndex != -1 && nextStudentIndex == -1){
nextStudentIndex = i;
}
}
list.add(nextStudentIndex, course);
}
這完全與Android無關。 PS list.add(list.indexOf(*放置*的地方),* element *) – shagi
[This](https://dzone.com/articles/hashmap-%E2%80%93-single-key-and)正是你所需要的。它使用一個HashMap在一個鍵下存儲多個值(在你的案例中)(在你的情況下爲學生)。 –
@AishwaryaTiwari難道他不能只在每個Student對象中放入'ArrayList'? –