唇劫. 2022-04-15 18:57 采纳率: 73.7%
浏览 30
已结题

java在集合,ArrayList集合完成,咋写

public class Test {
public static void main(String[] args) {

    ## 定义一个ArrayList集合,保存学生的对象。
    ## 快速实例化5个学生对象,依次存入集合中。
      ##  将集合中的对象进行排序,三门课程的总分最高分在前面,总分最低分在后面。
      ##  请实现,并输出集合数据验证。
      ##  算出语文的平均分输出。
       ## 语文分数低于平均分的学生信息输出。(对象里面的输出方法)
      ## 数学最高分的学生信息输出。
     

    ArrayList<Student> list=new ArrayList<Students>();
    Student s1=new Students("张三",81,72,99);
    Student s2=new Students("李四",82,72,55);
    Student s3=new Students("王五",83,73,46);
    Student s4=new Students("赵六",84,77,86);
    Student s5=new Students("田七",85,79,77);
    list.add(s1);
    list.add(s2);
    list.add(s3);
    list.add(s4);
    list.add(s5);
    Iterator<Students> iterator=list.iterator();
    while (iterator.hasNext()){
        System.out.println(iterator.next().PrintStu());
    }
    Collections.sort(list, new Comparator<Students>() {
        @Override
        public int compare(Students o1, Students o2) {
            int num1=o1.getYwscore()+o1.getYyscore()+o1.getYyscore();
            int num2=o2.getYwscore()+o2.getYyscore()+o2.getYyscore();
            if (num1>num2){
                return -1;
            }if (num1==num2){
                return 0;
            }
            return 1;
        }
    });
    System.out.println(list);
}

public class Student {

String name;
 int ywscore;
 int yyscore;
int sxscore;

public Students(){}
public Students(String name, int ywscore, int yyscore, int sxscore) {
    this.name = name;
    this.ywscore = ywscore;
    this.yyscore = yyscore;
    this.sxscore = sxscore;
}
public boolean PrintStu(){
    System.out.println("姓名:"+name+":语文成绩:"+ywscore+",英语成绩"+yyscore+",数学成绩:"+sxscore);
    return false;
}
public void setName(String name) {
    this.name = name;
}

public void setYwscore(int ywscore) {
    this.ywscore = ywscore;
}

public void setYyscore(int yyscore) {
    this.yyscore = yyscore;
}

public void setSxscore(int sxscore) {
    this.sxscore = sxscore;
}

public String getName() {
    return name;
}

public int getYwscore() {
    return ywscore;
}

public int getYyscore() {
    return yyscore;
}

public int getSxscore() {
    return sxscore;
}

}

  • 写回答

2条回答 默认 最新

  • 未聞花名丶 2022-04-15 19:54
    关注
    
    public class Student {
        String name;
        int ywscore;
        int yyscore;
        int sxscore;
    
        public Student() {
        }
    
        public Student(String name, int ywscore, int yyscore, int sxscore) {
            this.name = name;
            this.ywscore = ywscore;
            this.yyscore = yyscore;
            this.sxscore = sxscore;
        }
    
        public boolean PrintStu() {
            System.out.println("姓名:" + name + ":语文成绩:" + ywscore + ",英语成绩" + yyscore + ",数学成绩:" + sxscore);
            return false;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setYwscore(int ywscore) {
            this.ywscore = ywscore;
        }
    
        public void setYyscore(int yyscore) {
            this.yyscore = yyscore;
        }
    
        public void setSxscore(int sxscore) {
            this.sxscore = sxscore;
        }
    
        public String getName() {
            return name;
        }
    
        public int getYwscore() {
            return ywscore;
        }
    
        public int getYyscore() {
            return yyscore;
        }
    
        public int getSxscore() {
            return sxscore;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", ywscore=" + ywscore +
                    ", yyscore=" + yyscore +
                    ", sxscore=" + sxscore +
                    '}';
        }
    
        public static void main(String[] args) {
            ArrayList<Student> list = new ArrayList<Student>();
            Student s1 = new Student("张三", 81, 72, 99);
            Student s2 = new Student("李四", 82, 72, 55);
            Student s3 = new Student("王五", 83, 73, 46);
            Student s4 = new Student("赵六", 84, 77, 86);
            Student s5 = new Student("田七", 85, 79, 77);
            list.add(s1);
            list.add(s2);
            list.add(s3);
            list.add(s4);
            list.add(s5);
    
            // 将集合中的对象进行排序,三门课程的总分最高分在前面,总分最低分在后面。
            orderBySum(list);
    
            // 请实现,并输出集合数据验证。
            System.out.println("成绩从高到低:" + list);
    
            // 算出语文的平均分输出。
            double sum = 0;
            for (Student student : list) {
                sum += student.getYwscore();
            }
            double avg = sum / list.size();
            System.out.println("语文平均分:" + avg);
    
            // 语文分数低于平均分的学生信息输出。(对象里面的输出方法)
            for (Student student : list) {
                if (student.getYwscore() < avg) {
                    System.out.println("语文分数低于平均分的学生信息:" + student);
                }
            }
    
            // 数学最高分的学生信息输出。
            int max = 0;
            int index = 0;
            for (int i = 0; i < list.size(); i++) {
                Student student = list.get(i);
                if (student.getSxscore() > max) {
                    max = student.getSxscore();
                    index = i;
                }
            }
            System.out.println("数学最高分的学生信息:" + list.get(index));
        }
    
        private static void orderBySum(ArrayList<Student> list) {
            Collections.sort(list, new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    int num1 = o1.getSxscore() + o1.getYwscore() + o1.getYyscore();
                    int num2 = o2.getSxscore() + o2.getYwscore() + o2.getYyscore();
                    return num2 - num1;
                }
            });
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月15日
  • 已采纳回答 4月15日
  • 修改了问题 4月15日
  • 创建了问题 4月15日

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。