Answered step by step
Verified Expert Solution
Link Copied!

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 ArrayList courseStudents = 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

What was the role of the team leader? How was he or she selected?

Answered: 1 week ago

Question

How are members held accountable for serving in the assigned roles?

Answered: 1 week ago