看看能不能帮到你:
import java.util.ArrayList;
import java.util.List;
public class HelpOther {
List<Student> students;
List<Faculty> faculties;
Student selectStudent=null;
Faculty selectedFaculty=null;
public HelpOther(){
students=new ArrayList<>();
faculties=new ArrayList<>();
students.add(new Student("zhangsan"));
students.add(new Student("lisi"));
faculties.add(new Faculty("hehe"));
faculties.add(new Faculty("xixi"));
}
public static void main(String[] args) {
HelpOther test=new HelpOther();
test.AddEnrollment("zhangsan","lima");
HelpOther test1=new HelpOther();
boolean IsEx=test1.AddEnrollment("zhangsan","xixi");
if(IsEx){
System.out.println(test1.selectStudent.name);
System.out.println(test1.selectedFaculty.name);
}
}
public boolean AddEnrollment(String studentName,String facultyName) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).name.equals(studentName)) {
selectStudent = students.get(i);
}
}
for (int i = 0; i < faculties.size(); i++) {
if (faculties.get(i).name.equals(facultyName)) {
selectedFaculty = faculties.get(i);
}
}
if (selectedFaculty != null && selectStudent != null) {
System.out.println("查找成功");
return true;
} else {
System.out.println("查找失败");
return false;
}
}
}
class Student{
public String name;
public Student(String name){
this.name=name;
}
}
class Faculty{
public String name;
public Faculty(String name){
this.name=name;
}
}
