建立一个学生类(Student.class):
public class Student {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public Student(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public Student() {
super();
}
}
建立一个学生帮助类(StudentHandler.class):
public class StudentHandler {
private Student[] students;
private int count;
public StudentHandler(int max){
students = new Student[max];
count = 0;
}
public Student findStudent(String searchName){
int j ;
for (j = 0; j < count; j++) {
if(students[j].getUsername().equals(searchName)){
break;
}
}
if(j==count){
return null;
}else{
return students[j];
}
}
public void insertStudent(int id,String username,String password){
students[count] = new Student(id, username, password);
count++;
}
public boolean deleteStudent(String searchName){
int j;
for (j = 0; j < count; j++) {
if(students[j].getUsername().equals(searchName)){
break;
}
}
if(j==count)return false;
else{
for(int k=j;k students[k] = students[k+1];
}
count--;
return true;
}
}
public void displayStudent(){
for (int i = 0; i < count; i++) {
Student stu = students[i];
System.out.println(stu.getId()+" "+stu.getUsername()+" "+stu.getPassword());
}
}
}
最后就可以对学生进行增加,查找和展示操作(StudentApp.class):
public class StudentApp {
public static void main(String[] args) {
StudentHandler handler = new StudentHandler(100);
handler.insertStudent(1, "xuwei1", "123456");
handler.insertStudent(2, "xuwei2", "123456");
handler.insertStudent(3, "xuwei3", "123456");
handler.insertStudent(4, "xuwei4", "123456");
handler.insertStudent(5, "xuwei5", "123456");
handler.insertStudent(6, "xuwei6", "123456");
handler.insertStudent(7, "xuwei7", "123456");
handler.insertStudent(8, "xuwei8", "123456");
handler.insertStudent(9, "xuwei9", "123456");
handler.insertStudent(10, "xuwei10", "123456");
handler.deleteStudent("xuwei6");
handler.displayStudent();
}
}