Question: In a Java class method, how can I iterate through an arraylist and return every object so that it can be added to a new
In a Java class method, how can I iterate through an arraylist and return every object so that it can be added to a new arraylist in a different class?
For example:
public class Course { private ArrayListcourseStudents = new ArrayList<>(); // Students in a course that professor is teaching
//code to add student to courseStudents would be here public Student getStudent(){ if(!courseStudents.isEmpty()){ for(Student i : courseStudents){ return i; } } System.out.println("This instructor is not teaching any students."); return null; }} ------------------------------------
public class Student { private String name; // the student's name private String id; // the student's id number -----------------------------------
public class Instructor { private String name; private ArrayList profStudents = new ArrayList<>(); // Students the prof is teaching private ArrayList profCourses = new ArrayList<>(); // courses the prof is teaching //when a professor is assigned to a course to teach, I want all student objects to be added to the profStudents arraylist //this is the last line of code I have in this assigncourse method but it does not properly add a student object to the profStudents arraylist profStudents.add(course.getStudent());
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
