Java 根据得分和名称对备选方案进行排名
我想根据分数对一些备选方案进行排名,并打印每个备选方案的名称及其排名,我如何才能做到这一点 这里是MWE:Java 根据得分和名称对备选方案进行排名,java,arrays,sorting,Java,Arrays,Sorting,我想根据分数对一些备选方案进行排名,并打印每个备选方案的名称及其排名,我如何才能做到这一点 这里是MWE: import java.util.Arrays; public class Ranking { public static void main(String[] args) { //The score of the alternatives : double [] score = new double [4] ; score[0] = 6.75 ; score[1] = 9.0 ; sc
import java.util.Arrays;
public class Ranking {
public static void main(String[] args) {
//The score of the alternatives :
double [] score = new double [4] ;
score[0] = 6.75 ;
score[1] = 9.0 ;
score[2] = 6.75 ;
score[3] = 5.50;
//The name of the alternatives :
String [] name = new String [4] ;
name[0] = "a1";
name[1] = "a2";
name[2] = "a3";
name[3] = "a4";
//Descending Sorting the score array like this :
for(int i=0;i<4;i++)
score[i]= - score[i];
Arrays.sort(score);
for(int i=0;i<4;i++)
score[i]= - score[i];
//print the result
for(int i=0;i<4;i++)
System.out.println(score[i] + " rank = " + (i+1));
}
//result :
//9.0 rank = 1
//6.75 rank = 2
//6.75 rank = 3
//5.5 rank = 4
我如何才能做到这一点?您可以对单个数组进行排序,这样结果就不能在name-rank之间映射 在java中,通常的方法是将数据抽象到对象并对其进行操作: 声明模型类:
public YourModel {
private String name;
private double score;
public Rank(String name, double score) {
this.name = name;
this.score = score;
}
// you create your getters/ setters
}
将数据添加到阵列:
YourModel[] models = new YourModel[4];
models[0] = new YourModel("a1", 6.75D);
。。。添加其他模型
对阵列进行排序:
Arrays.sort(models, Comparator.comparing(YourModel::getScore));
完成此步骤后,您将对数组进行排序:
[{"a2", 9.0} ....]
您可以打印您的结果:
for(int i=0;i<4;i++)
System.out.println(models[i].getName() + " rank = " + (i+1));
}
对于(int i=0;i您应该使用Map
进行此操作
请尝试以下示例:
Map<String, Double> map = new HashMap<>();
map.put("a1", 6.75);
map.put("a2", 9.0);
map.put("a3", 6.8);
map = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
//Iterate through the map
Map Map=newhashmap();
地图放置(“a1”,6.75);
地图放置(“a2”,9.0);
地图.付诸表决(“a3”,6.8);
map=map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(collector.toMap)(
Map.Entry::getKey,Map.Entry::getValue,(e1,e2)->e1,LinkedHashMap::new));
//遍历地图
你将以相反的顺序得到答案
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
用于按值排序,而LinkedHashMap::new
用于维护Map
的排序。当您必须对某些内容进行排序时,您应该查看TreeSet
或TreeMap
结构。实际上,数组
不是排序的主要功能
似乎所有对象都有唯一的名称
,然后必须使用混响比较器的树集
。因此,当您迭代集
时,您会得到所需的顺序:
final class Model {
private final String name;
private final double score;
public Model(String name, double score) {
this.name = name;
this.score = score;
}
}
final Comparator<Model> reverceScore = (d1, d2) -> {
if (d1.name.equalsIgnoreCase(d2.name))
return 0;
int res = Double.compare(d2.score, d1.score);
return res != 0 ? res : d1.name.compareTo(d2.name);
};
Set<Model> data = new TreeSet<>(reverceScore);
data.add(new Model("a1", 6.75));
data.add(new Model("a2", 9));
data.add(new Model("a3", 6.75));
data.add(new Model("a4", 5.5));
int rank = 1;
for(Model model : data)
System.out.println("rank: " + rank++ + " - name: " + model.name + ", score: " + model.score);
最终类模型{
私有最终字符串名;
私人决赛双分;
公共模型(字符串名称,双倍分数){
this.name=名称;
这个分数=分数;
}
}
最终比较器混响核心=(d1,d2)->{
if(d1.名称.相等信号案例(d2.名称))
返回0;
int res=双重比较(d2分数,d1分数);
返回res!=0?res:d1.name.compareTo(d2.name);
};
设置数据=新树集(ReverseCore);
添加数据(新型号(“a1”,6.75));
添加数据(新模型(“a2”,9));
添加数据(新型号(“a3”,6.75));
添加数据(新型号(“a4”,5.5));
int秩=1;
用于(模型:数据)
System.out.println(“秩:“+秩++”—名称:“+model.name+”,分数:“+model.score”);
你很难帮助你,因为如果没有缩进,程序几乎不可读。我建议你从Java代码中退一步。试着用文字描述解决问题所需的步骤。一旦你能向另一个人清楚地解释这些步骤,然后试着将它们翻译成代码。谢谢你的建议。谢谢你或者,我尝试运行它,但在sortedModels行上出现了一个错误“不兼容的类型void无法转换为YourModel[][]”,我如何解决这个问题?现在它可以工作了,但我得到了升序排序,我如何得到逆序?将.reversed()
添加到comparator:comparator.comparating(YourModel::getScore).reversed()
加油!很高兴它能帮上忙这正是我想要的,我以前从未使用过Map,这是一个好的开始,谢谢!@RedOne欢迎。如果问题解决了,你可以接受答案。我还有最后一个问题,如何打印结果?@RedOne如果你只想看到你可以使用的值System.out.println(Map);
。要处理每个值,您需要在映射中迭代。您可以用谷歌搜索或查看
final class Model {
private final String name;
private final double score;
public Model(String name, double score) {
this.name = name;
this.score = score;
}
}
final Comparator<Model> reverceScore = (d1, d2) -> {
if (d1.name.equalsIgnoreCase(d2.name))
return 0;
int res = Double.compare(d2.score, d1.score);
return res != 0 ? res : d1.name.compareTo(d2.name);
};
Set<Model> data = new TreeSet<>(reverceScore);
data.add(new Model("a1", 6.75));
data.add(new Model("a2", 9));
data.add(new Model("a3", 6.75));
data.add(new Model("a4", 5.5));
int rank = 1;
for(Model model : data)
System.out.println("rank: " + rank++ + " - name: " + model.name + ", score: " + model.score);