Students

该博客主要展示了如何使用Java编程实现从学生列表或映射中按平均分数筛选学生,并根据年龄进行排序。提供了两个方法,一个用于按指定分数获取学生列表,另一个返回一个映射,其中键为平均分数,值为对应分数的学生列表。
摘要由CSDN通过智能技术生成
package step1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class Students {
	private List<Student> stuList;
	private Map<Integer,Student> stuMap;
	public Students(List<Student> stuList) {
		super();
		this.stuList = stuList;
	}
	public Students(Map<Integer, Student> stuMap) {
		super();
		this.stuMap = stuMap;
	}
	public Students() {
		super();
	}
	public List<Student> getByAvgScore(double score) {
		List<Student> reList = new ArrayList<Student>();
		Student students[] = new Student[stuList.size()];
		int cont=0;
		for(Student s:stuList) {
			if(s.getAvgScore()==score) {
				students[cont]=s;
				cont++;
			}
		}
		for(int i=0;i<cont-1;i++)
        {
            for(int j=0;j<cont-1-i;j++)
            {
                if(students[j].getAge()>students[j+1].getAge())
                {
                    Student temp=students[j];
                    students[j]=students[j+1];
                    students[j+1]=temp;
                }
            }
        }
		for(int i=0;i<cont;i++) {
			reList.add(students[i]);
		}
		return reList;
	}
	public Map<Double, List<Student>> getByAvgScores(){
		Map<Double,List<Student>> reMap=new HashMap<>();
		double avgArray[] = new double[stuList.size()];
		int cont=0;
		Set<Map.Entry<Integer, Student>> entrySet = stuMap.entrySet(); 
		for(Map.Entry<Integer, Student> item : entrySet) {
			double temp = item.getValue().getAvgScore();
			boolean flag=true;
			for(int i = 0;i<cont;i++) {
				if(avgArray[i]==temp) flag=false;
			}
			if(flag) {
				avgArray[cont]=temp;
				cont++;
			}
		}
		for(double temp:avgArray) {
			reMap.put(temp, getByAvgScore(temp));
		}
		return reMap;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值