Reputation: 219
So I have several "student" objects in an arraylist, each student has an arraylist of courses. how do I access all of the courses from a particular student? Thanks in advance.
Scanner studentCourse = new Scanner(System.in);
int j = 0;
System.out.println("Which students course(s) do you want to view?");
for(Student l:listStudent) {
System.out.print(j+1 + ")");
System.out.println(l);
j++;
}
int course = studentCourse.nextInt();
int k = s.listCourse.size();//need this to be the size of the correct array list
int l = 0;
while( l < k ){
System.out.println(s.listCourse.get(0));//need this to print all of the elements in the correct array list
}
break;
public class Student {
String studentFirstName, studentLastName;
int studentID;
double studentGPA;
ArrayList<Course> listCourse = new ArrayList<>();
}
Upvotes: 0
Views: 86
Reputation: 297
You can use getter for course. By using you can do following:
int len = s.listCourse.size();
for(int i = 0; i<=len; i++) {
System.out.println(s.listCourse.get(i).getCourse());
}
Upvotes: 0
Reputation: 8199
You can use getter
& setter
methods in your Student
class or just you use the for
loop, as below.
for(Course course : s.listCourse) {
System.out.println(course);
}
Upvotes: 1
Reputation: 18868
Your question is very much understandable, but not your code.
Student student = studentList.get(indexPosition);
//ArrayList gives this flexibility and that's the advantage of ArrayList over other Lists
if(student.getCoursesList()!=null && !student.getCoursesList().isEmpty()){
for(Course course: student.getCoursesList()){
System.out.println(course);
}
}
Upvotes: 1
Reputation: 39307
Look at using for
for (Course course : s.listCourse) {
System.out.println(course);
}
Upvotes: 1