package test;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @Author Ellrien 2016年4月14日 上午13:25:21
*/
public class Student {
private String name;// 学生姓名
private String no;// 学生学号
private String sex;// 性别
public Student(String name, String no, String sex) {
super();
this.name = name;
this.no = no;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public static void main(String[] args) {
Student s1 = new Student("张三", "20160001", "男");
Student s2 = new Student("张四", "20160002", "男");
Student s3 = new Student("张五", "20160003", "男");
Student s4 = new Student("张六", "20160004", "女");
Student s5 = new Student("张七", "20160005", "女");
Student s6 = new Student("张八", "20160006", "女");
//添加学生到学生列表里
List
StudentUtil.addStudent(students, s1);
StudentUtil.addStudent(students, s2);
StudentUtil.addStudent(students, s3);
StudentUtil.addStudent(students, s4);
StudentUtil.addStudent(students, s5);
StudentUtil.addStudent(students, s6);
//找到学号,删除掉这个学生。
StudentUtil.deleteStudent(students, "20160005");
}
static class StudentUtil {
public static void addStudent(List
students.add(s);
}
public static void deleteStudent(List
Student student = null;
for (Student s : students) {
if (no != null && s.getNo().equals(no)) {
student = s;
break;
}
}
if (student != null) {
students.remove(student);
}
}
}
}
//Student.java
public class Student {
private int id;
private String name;
private String grade;//班级
public Student(){}
public Student(int id, String name, String grade) {
super();
this.id = id;
this.name = name;
this.grade = grade;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "学号 [id=" + id + ", 姓名=" + name + ", 班级=" + grade
+ "]";
}
}
//StudentUtil.java
import java.util.*;
public class StudentUtil {
public Student getStuById( List
Student student = new Student();
if(stuList!=null){
Iterator
while(iterator.hasNext()){
student = iterator.next();
if(student.getId()==id){
return student;
}
}
}
else{
System.out.println("没有学生!");
}
return student;
}
}
//Test.java
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
List
Student stu1 = new Student(201101,"王同学","2班");
Student stu2 = new Student(201105,"张同学","2班");
Student stu3 = new Student(201112,"张张同学","3班");
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu2);
System.out.println(new StudentUtil().getStuById(stuList,201112).toString());
}
}
好好学学java吧,这是饭碗啊。geek99. com