使用数组操作实体类

发布时间 2023-09-29 17:32:13作者: DogLeftover
  • 编写实体类
public class Student {

    String name; // 姓名

    String sex; // 性别

    int sno; // 学号

    int score; // 成绩

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", sno=" + sno +
                ", score=" + score +
                '}';
    }

}
  • 编写方法
public class MyArrar {

    Student[] num = new Student[100];

    private int len=0;

    public int size(){
        return len;
    }

    //添加一个学生,放到数组的最后
    public int add(Student stu){
        if(len<num.length){
            num[len]=stu;
            len++;
            return 1;
        } else {
            return -1;
        }
    }

    //显示所有学生的信息
    public void show(){
        System.out.println("学号\t姓名\t性别\t成绩");
        for (int i = 0; i < len; i++) {
            Student stu=num[i];
            System.out.println(" " + stu.getSno() + "  " + stu.getName()
                    + "  " + stu.getSex() + "  " + stu.getScore());
        }
    }

    //根据下标删除
    // [0][1][]
    public int remove(int index){
        if(index>len-1){
            return -1;  // 数组为空或下标大于数组长度
        }else {
            for(int i=index; i<len; i++){
                num[i]=num[i+1];
            }
            len--;
            return 1;
        }
    }

    //根据学号删除
    // [][][6][][]
    public int removeSno(int sno){
        try {
            int index = 0;
            for(int i=0; i<len; i++){
                if(num[i].getSno()==sno){
                    index=i;
                    break;
                }
            }
            // del
            for(int i=index; i<len; i++){
                num[i]=num[i+1];
            }
            len--;
            return 1;
        }catch (ArrayIndexOutOfBoundsException e){
            return -1;
        }
    }

    //根据学号查找学生
    public Student findBySno(int sno) {
        int index = 0;
        for(int i=0; i<len; i++){
            if(num[i].getSno()==sno){
                index=i;
                break;
            }
        }
        return num[index];
    }

    //根据姓名查找学生
    public Student findByName(String name){
        int index = 0;
        for(int i=0; i<len; i++){
            if(num[i].getName()==name){
                index=i;
                break;
            }
        }
        return num[index];
    }

    //成绩升序排序
    public void sortAsc(){
        for(int b = len-1; b>0; b--){
            Student tmp;
            int i=0;
            for(int a = b; a>0; a--){
                if(num[i].getScore()>num[i+1].getScore()){
                    tmp = num[i];
                    num[i] = num[i+1];
                    num[i+1] = tmp;
                }
                i++;
            }
        }
    }

    //假如已经按照成绩升序排序之后,将学生添加到对应的位置,保证数组仍然是升序
    public void addSortAsc(Student stu){
        for (int i = 0; i < len; i++) {
            if (stu.getScore() < num[i].getScore()){
                // 后移
                for(int j=len; j>i; j--){
                    num[j]=num[j-1];
                }
                // 长度++
                len++;
                // 插入位置0
                num[i]=stu;
                // 结束整个循环
                break;
            }else { // 大于等于ints[0]
                // 不成立则continue,进入下一轮比较
                continue;
            }
        }
        // 当插入的值比数组中最大的值还大时,插入到最后
        if(stu.getScore() >= num[len-1].getScore()){
            num[len]=stu;
            len++;
        }
    }

    //成绩降序排序
    public void sortDesc(){
        for(int b = len-1; b>0; b--){
            Student tmp;
            int i=0;
            for(int a = b; a>0; a--){
                if(num[i].getScore()<num[i+1].getScore()){
                    tmp = num[i];
                    num[i] = num[i+1];
                    num[i+1] = tmp;
                }
                i++;
            }
        }
    }

    //假如已经按照成绩降序排序之后,将学生添加到对应的位置,保证数组仍然是降序
    //[90][80][70][]
    //[0][1][2][]
    public void addSortDesc(Student stu){
        for (int i = 0; i < len; i++) {
            if (stu.getScore() > num[i].getScore()){
                // 后移
                for(int j=len; j>i; j--){
                    num[j]=num[j-1];
                }
                // 长度++
                len++;
                // 插入位置0
                num[i]=stu;
                // 结束整个循环
                break;
            }else { // 小于等于ints[0]
                // 不成立则continue,进入下一轮比较
                continue;
            }
        }
        // 当插入的值比数组中最小的值还小时,插入到最后
        if(stu.getScore() <= num[len-1].getScore()){
            num[len]=stu;
            len++;
        }
    }

}
  • 测试
public class test {
    public static void main(String[] args) {
        MyArrar myArrar=new MyArrar();

        Student student=new Student();
        student.setName("张三");
        student.setSex("男");
        student.setSno(3);
        student.setScore(68);
        myArrar.add(student);
        Student student1=new Student();
        student1.setName("张四");
        student1.setSex("女");
        student1.setSno(9);
        student1.setScore(95);
        myArrar.add(student1);
        Student student2=new Student();
        student2.setName("李三");
        student2.setSex("男");
        student2.setSno(6);
        student2.setScore(72);
        myArrar.add(student2);
        // len
        System.out.println(myArrar.size());
        // 显示
        myArrar.show();

        // 删除
//        myArrar.remove(1);
//        myArrar.show();
//        myArrar.removeSno(6);
//        myArrar.show();

        // 查找
        System.out.println(myArrar.findBySno(6));
        System.out.println(myArrar.findByName("李三"));

        // 排序
        System.out.println("--- 成绩升序 ---");
        myArrar.sortAsc();
        myArrar.show();
        System.out.println("--- 插入一条数据 ---");
        Student student3=new Student();
        student3.setName("李五");
        student3.setSex("男");
        student3.setSno(7);
        student3.setScore(95);
        myArrar.addSortAsc(student3);
        myArrar.show();

        System.out.println("--- 成绩降序 ---");
        myArrar.sortDesc();
        myArrar.show();
        System.out.println("--- 插入一条数据 ---");
        Student student4=new Student();
        student4.setName("李六");
        student4.setSex("男");
        student4.setSno(2);
        student4.setScore(68);
        myArrar.addSortDesc(student4);
        myArrar.show();

    }
}