• ArrayList 的用法


    定义一个学生类Student,属性有age,name,score,classNum

    有如下:

    list.add(new Student("Tom",18,100,"class05"));
    list.add(new Student("Jerry",22,70,"class04"));
    list.add(new Student("Owen",25,90,"class05"));
    list.add(new Student("Jim",30,80,"class05"));
    list.add(new Student("Steve",28,66,"class06"));
    list.add(new Student("Kevin",24,100,"class04"));

    在这个list的基础上,完成下列要求:

    1、统计出总班级数

    2、统计每个班的平均分和人数

    3、按学生的成绩排名(成绩相同的按年龄排名)

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class Student {
         int age;
         String name;
         int score;
         String classNum;
         Student(){
             
         }
         Student(String name,int age,int score,String classNum)
         {
             this.age=age;
             this.name=name;
             this.score=score;
             this.classNum=classNum;
         }
         
         public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getScore() {
            return score;
        }
        public void setScore(int score) {
            this.score = score;
        }
        public String getClassNum() {
            return classNum;
        }
        public void setClassNum(String classNum) {
            this.classNum = classNum;
        }
        
        public static void main(String[] args) {
            List <Student>list=new ArrayList<>();
            list.add(new Student("Tom",18,100,"class05"));
            list.add(new Student("Jerry",22,70,"class04"));
            list.add(new Student("Owen",25,90,"class05"));
            list.add(new Student("Jim",30,80,"class05"));
            list.add(new Student("Steve",28,66,"class06"));
            list.add(new Student("Kevin",24,100,"class04"));
            //统计班级
            Set <String> set=new HashSet<>();
            for(int i=0;i<list.size();i++)
            {  
                
                set.add(list.get(i).getClassNum());
                
                
            }
            System.out.println("总共有:"+set.size());
            //每班平均分、人数
            Object[] array=set.toArray();
            
            for(int j=0;j<set.size();j++)
            {   int sum=0;
                int count=0;
                for(int i=0;i<list.size();i++)
                {
                  if(list.get(i).getClassNum().equals(array[j])){
                      sum+=list.get(i).getScore();
                      count++;
                  }
                }
                System.out.println(array[j]+" 平均分:"+(sum/count)+"  人数:"+count);
            }
            //排序
            int tmp=0;
            for(int i=0;i<list.size();i++)
            {
                for(int j=i+1;j<list.size();j++)
                {
                    if(list.get(i).score>list.get(j).score)
                    {
                        tmp=list.get(i).score;
                        list.get(i).score=list.get(j).score;
                        list.get(j).score=tmp;
                    }
                }
                
            }
            
            for(int i=0;i<list.size();i++)
            {
                for(int j=i+1;j<list.size();j++)
                {
                    if(list.get(i).score==list.get(j).score)
                    {
                        if(list.get(i).age>list.get(j).age)
                        {
                            tmp=list.get(i).age;
                            list.get(i).age=list.get(j).age;
                            list.get(j).age=tmp;
                        }
                    }
                }
                
            }
            for(int i=0;i<list.size();i++)
            {
                System.out.println(list.get(i).getName()+" "+list.get(i).age+":  "+list.get(i).getScore());
            }
            
        }
    
    }

     ArrayList的用法和数组差不多,只是有很多的API,更方便我们解决问题

  • 相关阅读:
    eclipse写javaee的时候js文件新增函数找不到
    baidu春招题:熊回家
    java自定义容器排序实现接口
    Thread主体和执行主体
    jqurey定位 id
    c中二维数组与指针访问
    ubuntu上浏览器上不了网
    前端经典面试题
    HTML,CSS,JS试题
    CSS3实现文字浮雕效果,镂刻效果,火焰文字
  • 原文地址:https://www.cnblogs.com/xiaojuzibuxiao/p/7725939.html
Copyright © 2020-2023  润新知