问题描述
目前我有两个班级.一个 Classroom 类和一个 School 类.我想在 School 类 public void showClassRoomDetails
中编写一个方法,该方法仅使用教师姓名来查找教室详细信息.
Currently I have two classes. A Classroom class and a School class. I would like to write a method in the School class public void showClassRoomDetails
which would find the classroom details by only using the teacherName.
例如TeacherName = Daniel className = Science
e.g.teacherName = Daniel className = Science
teacherName = Bob className = Maths
teacherName = Bob className = Maths
所以当我输入 Bob 时,它会打印出 Bob 和 Maths
so when I input Bob, it would print out Bob and Maths
很多,谢谢
public class Classroom
{
private String classRoomName;
private String teacherName;
public void setClassRoomName(String newClassRoomName)
{
classRoomName = newClassRoomName;
}
public String returnClassRoomName()
{
return classRoomName;
}
public void setTeacherName(String newTeacherName)
{
teacherName = newTeacherName;
}
public String returnTeacherName()
{
return teacherName;
}
}
import java.util.ArrayList;
public class School
{
private ArrayList<Classroom> classrooms;
private String classRoomName;
private String teacherName;
public School()
{
classrooms = new ArrayList<Classroom>();
}
public void addClassRoom(Classroom newClassRoom, String theClassRoomName)
{
classrooms.add(newClassRoom);
classRoomName = theClassRoomName;
}
public void addTeacherToClassRoom(int classroomId, String TeacherName)
{
if (classroomId < classrooms.size() ) {
classrooms.get(classroomId).setTeacherName(TeacherName);
}
}
public void showClassRoomDetails
{
//loop
System.out.println(returnClassRoomName);
System.out.println(returnTeacherName);
}
}
推荐答案
您真的需要这里的列表吗?包含课堂的地图 - 教师协会对您想要实现的目标更有帮助.但是你的代码中也有一些奇怪的地方:例如,你为什么在你的 School 类中持有一个 classRoomName 和 TeacherName 作为实例变量?
Do you really need a list here ?A Map holding classroom - teacher associations would be more helpful for what you're trying to achieve.But there are also strange things in your code: for instance, why do you hold a classRoomName and teacherName as instance variables in your School class ?
这篇关于在数组列表中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!