Returning the index number of an Arraylist in Java
Posted
by
Daniel
on Stack Overflow
See other posts from Stack Overflow
or by Daniel
Published on 2011-01-05T14:50:53Z
Indexed on
2011/01/05
14:53 UTC
Read the original article
Hit count: 239
I would like my method public void showClassRoomDetails(String teacherName)
to return the Arraylist index number using the teacherName.
Thanks
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(String teacherName)
{
for (Classroom classroom : this.classrooms)
{
if (classroom.returnTeacherName().equals(teacherName))
{
System.out.println(classroom.returnClassRoomName());
System.out.println(classroom.returnTeacherName());
break;
}
}
}
}
© Stack Overflow or respective owner