数组操作对象

建立一个学生类(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<count;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();

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值