toString Iteration of an ArrayList
Posted by kevaughn
Last Updated: May 27, 2014
public class School {
    
    private String schoolName;
    private String schoolLocation;
    private ArrayList <Student> listOfStudents;
    private ArrayList <Instructor> listOfInstructors;

    public School(String schoolName, String schoolLocation, ArrayList<Student> listOfStudents, ArrayList<Instructor> listOfInstructors) {
        this.schoolName = schoolName;
        this.schoolLocation = schoolLocation;
        this.listOfStudents = listOfStudents;
        this.listOfInstructors = listOfInstructors;
    }
    public String getSchoolName() {
        return schoolName;
    }

    public String getSchoolLocation() {
        return schoolLocation;
    }

    public ArrayList<Student> getListOfStudents() {
        return listOfStudents;
    }

    public ArrayList<Instructor> getListOfInstructors() {                             
        return listOfInstructors;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    public void setSchoolLocation(String schoolLocation) {
        this.schoolLocation = schoolLocation;
    }

    public void setListOfStudents(ArrayList<Student> listOfStudents) {
        this.listOfStudents = listOfStudents;
    }

    public void setListOfInstructors(ArrayList<Instructor> listOfInstructors) {
        this.listOfInstructors = listOfInstructors;
    }

    @Override
    public String toString() {
        return "school Name: \t\t" + schoolName + ", school Location: \t\t" + schoolLocation + ", "
                + "list Of Students: \t\t" + listOfStudents + 
                
                ", list Of Instructors: \t\t" + listOfInstructors;
    }
     
   
}
the toString method needs to iterate through the list of students and the list of instructors and include their information in the returned string. 

That's the instruction but mi know mi could use the for loop with index to do it. But should i list put the name of the students/instructors and them loop through them using ..has.Next. Let me know or see an example of the above code. Respect.
Related Content