package day0603; import java.util.Arrays; /* 持久层 */ public class StudentDao { private Student[] students = {}; public String addStudent(Student student) { students = Arrays.copyOf(this.students, students.length + 1); students[students.length-1] = student; return "添加成功"; } public Student[] getStudents(){ return students; } public String deleteStudentById(String id) { int index = getIndex(id); students[index] = students[students.length-1]; students = Arrays.copyOf(students,students.length-1); return "删除成功"; } public String updateStudent(Student student) { String id = student.getId(); int index = getIndex(id); students[index] = student; return "修改成功"; } public Student selectOneById(String id) { int index = getIndex(id); return students[index]; } public int getIndex(String id) { for (int i = 0; i < students.length; i++) { if (students[i].getId().equals(id)) { return i; } } return -1; } }将该代码中的student全部替换为teacher
时间: 2024-02-14 12:10:38 浏览: 95
java代码-import java.util.Scanner;
5星 · 资源好评率100%
替换后的代码如下:
```java
package day0603;
import java.util.Arrays;
/* 持久层 */
public class TeacherDao {
private Teacher[] teachers = {};
public String addTeacher(Teacher teacher) {
teachers = Arrays.copyOf(this.teachers, teachers.length + 1);
teachers[teachers.length-1] = teacher;
return "添加成功";
}
public Teacher[] getTeachers(){
return teachers;
}
public String deleteTeacherById(String id) {
int index = getIndex(id);
teachers[index] = teachers[teachers.length-1];
teachers = Arrays.copyOf(teachers,teachers.length-1);
return "删除成功";
}
public String updateTeacher(Teacher teacher) {
String id = teacher.getId();
int index = getIndex(id);
teachers[index] = teacher;
return "修改成功";
}
public Teacher selectOneById(String id) {
int index = getIndex(id);
return teachers[index];
}
public int getIndex(String id) {
for (int i = 0; i < teachers.length; i++) {
if (teachers[i].getId().equals(id)) {
return i;
}
}
return -1;
}
}
```
阅读全文