一个类,包含成员变量、构造方法和成员方法(属性对应的set/get方法和其他成员方法)
/** * 定义一个Student类{学号、姓名、年龄、性别} * * @author * */ public class Student { private int sid;//编号 private String sname;//姓名 private int age;//年龄 private String gender;//性别 public Student() { } public Student(int sid, String sname, int age, String gender) { super(); this.sid = sid; this.sname = sname; this.age = age; this.gender = gender; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return this.getSid() + "\t" + this.getSname() + "\t" + this.getAge() + "\t" + this.getGender(); } }对学生进行增删改查
/** * 针对学生操作类(数组存储、及根据条件增删改查) * * @author * */ public class MyStudentUtil { /** * 获取学生实际人数(数组实际长度) * * @param stuArray */ private static int getStuCount(Student[] stuArray) { int i = 0; for (; i < stuArray.length; i++) { Student s = stuArray[i]; if (s == null) { break; } } return i; } /** * 增加学生 * * @param stu * @param stuArray */ public static void addStu(Student stu, Student[] stuArray) { int stuCount = getStuCount(stuArray); stuArray[stuCount] = stu; } /** * 查看所有学生信息 * * @param stuArray */ public static void printStus(Student[] stuArray) { System.out.println("学号\t" + "姓名\t" + "年龄\t" + "性别\t"); for (int i = 0; i < stuArray.length; i++) { Student s = stuArray[i]; if (s != null) { System.out.println(s.toString()); } else { break; } } } /** * 根据学号修改学生信息 修改成功返回true,否则返回false * * @param stuArray * @param sid * @param sname * @param age * @param gender */ public static boolean updateStuBySid(Student[] stuArray, int sid, String sname, int age, String gender) { Student s = null; if ((s = selectStuBySid(sid, stuArray)) != null) { s.setSname(sname); s.setAge(age); s.setGender(gender); return true; } else { return false; } } /** * 根据学号查看某一个学生信息 查找到该学生返回该学生对象,否则返回null * * @param sid * @param atuArray */ public static Student selectStuBySid(int sid, Student[] stuArray) { for (int i = 0; i < getStuCount(stuArray); i++) { Student s = stuArray[i]; if (s.getSid() == sid) { return s; } else { continue; } } return null; } /** * 根据性别查看同性别的所有学生 * * @param gender * @param stuArray */ public static void selectStusByGender(String gender, Student[] stuArray) { for (int i = 0; i < getStuCount(stuArray); i++) { Student s = stuArray[i]; if (gender.equals(s.getGender())) { System.out.println(s.toString()); } } } /** * 根据学号删除某一个学生 * * @param sid * @param stuArray * @return */ public static boolean delStusBySid(int sid, Student[] stuArray) { int index = -1; for (int i = 0; i < getStuCount(stuArray); i++) { if (sid == stuArray[i].getSid()) { index = i; break; } } if (index == -1) { return false; } int j = index; for (; j <= getStuCount(stuArray) + index; j++) { stuArray[j] = stuArray[j + 1]; } stuArray[j + 1] = null; return true; } // 交换学生位置 private static void swap(Student a, Student b) { Student t = a; a = b; b = t; } /** * 根据学号升序排序 * * @param stuArray */ public static void sidAscSortStus(Student[] stuArray) { for (int i = 0; i < getStuCount(stuArray); i++) { for (int j = 1; j < getStuCount(stuArray) - i; j++) { if (stuArray[j - 1].getSid() > stuArray[j].getSid()) { swap(stuArray[j - 1],stuArray[j]); Student t = stuArray[j - 1]; stuArray[j - 1] = stuArray[j]; stuArray[j] = t; } } } } /** * 根据学号降序排序(冒泡) * * @param stuArray */ public static void sidDescSortStus(Student[] stuArray) { for (int i = 0; i < getStuCount(stuArray); i++) { for (int j = 1; j < getStuCount(stuArray) - i; j++) { if (stuArray[j - 1].getSid() < stuArray[j].getSid()) { swap(stuArray[j - 1],stuArray[j]); Student t = stuArray[j - 1]; stuArray[j - 1] = stuArray[j]; stuArray[j] = t; } } } } }