gpt4 book ai didi

用于对特定对象数组进行排序的 Java Comparator 类

转载 作者:行者123 更新时间:2023-11-29 09:41:14 24 4
gpt4 key购买 nike

我有一个 float 组和一个字符串数组。每个浮点值与特定字符串匹配。我想使用以下方式对保留自己的字符串的 float 组进行排序:

public static <T> void sort(T[] a,Comparator<? super T> c)

代码如下:

public class ResultVoiceObject
{

private String frase;
private float ranking;
public ResultVoiceObject(String f, float r)
{
this.frase=f;
this.ranking= r;
}
}
public class VoiceRecognitionDemo extends Activity
{

// Populate the wordsList with the String values the recognition engine thought it heard
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//il Ranking
float[] score= data.getFloatArrayExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES);

ResultVoiceObject[] risultati= new ResultVoiceObject[score.length];
for (i=0; i<risultati.length;i++)
{
risultati[i]=new ResultVoiceObject(matches.get(i), score[i]);
}
ResultVoiceObject[] risultatiDaOrdinare= risultati; // risultati contais ResultVoiceObject elements
/*sorting*/
}

如何排序并保留自己的字符串?

非常感谢。

最佳答案

ResultVoiceObject[] objects = ...
Arrays.sort(objects, new Comparator<ResultVoiceObject>() {

@Override
public int compare(ResultVoiceObject arg0, ResultVoiceObject arg1) {
return Float.compare(arg0.getRanking(), arg1.getRanking());
}

});

关于用于对特定对象数组进行排序的 Java Comparator 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17164117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com