Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 ArrayListprofStudents = 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started