Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在“最佳成绩历史记录”活动中添加得分最高的球员的姓名?_Java_Android_Android Studio_Arraylist_Collections - Fatal编程技术网

Java 如何在“最佳成绩历史记录”活动中添加得分最高的球员的姓名?

Java 如何在“最佳成绩历史记录”活动中添加得分最高的球员的姓名?,java,android,android-studio,arraylist,collections,Java,Android,Android Studio,Arraylist,Collections,我有一个带测验的申请表和一个活动,我想让那些得分最高的球员进入前5名。 我有两个ArrayList,它存储一个的分数,另一个存储玩家的名字。 我设法把分数列在名单上,但我不知道如何在同龄人分数旁边找到正确的名字 我所拥有的形象: 以及我正在寻找的图像: 我试过使用一个映射,但我不知道不能存储两个相同的键,所以这是不可能的,但可能有一种方法可以使用某种带索引的arraylist,但它可以有两个不同的信息,我不知道 我的2个ArrayList: public static ArrayList<

我有一个带测验的申请表和一个活动,我想让那些得分最高的球员进入前5名。 我有两个ArrayList,它存储一个的分数,另一个存储玩家的名字。 我设法把分数列在名单上,但我不知道如何在同龄人分数旁边找到正确的名字

我所拥有的形象:

以及我正在寻找的图像:

我试过使用一个映射,但我不知道不能存储两个相同的键,所以这是不可能的,但可能有一种方法可以使用某种带索引的arraylist,但它可以有两个不同的信息,我不知道

我的2个ArrayList:

public static ArrayList<Integer> scoresList = new ArrayList<>();

public static ArrayList<String> namesList = new ArrayList<>();

感谢

制作一个类来表示分数/姓名条目:

public class ScoreEntry implements Comparable<ScoreEntry> {
    public final String name;
    public final int score;

    public ScoreEntry (String name, int score){
        this.name = name;
        this.score = score;
    }

    public int compareTo (ScoreEntry other){
        return Integer.signum(score - other.score);
    }
}
编辑:如果您想通过其他方式进行排序,您需要一个自定义比较器。我将其改编自,其中考虑了资本化

Comparator<ScoreEntry> nameComparator = new Comparator<>(){
    public int compare(ScoreEntry first, ScoreEntry second) {
        int res = String.CASE_INSENSITIVE_ORDER.compare(first.name, second.name);
        if (res == 0)
            res = first.name.compareTo(second.name);
        return res;
    }
}

不像写这样的东西那么简单:
first.setText(“最好的分数是:“+scoresList.get(0)+”-“+namesList.get(0))?您可以使用ListView(或RecyclerView)和适配器作为wellBut nameList进行此操作。get(0)与scoreList.get(0)不匹配因为有一种排序允许我访问scoreList@shermano的最大值,为什么你有一个
nameList
scoreList
,而你可以有一个
playerList
和一个
class Player{private String name;private long score;}
,然后创建一个
比较器
以按分数值对其排序?在我的ArrayList中,添加分数输入值后如何访问分数或名称?和.添加(新的分数条目(..);增加价值的好方法是什么?谢谢(对不起,我是新开发的)这是添加它们的好方法。要获取第一个列表,请执行以下操作:
list.get(0).score
。例如,我刚刚找到了如何进行排序的方法,但非常感谢,很抱歉,您可以向我展示如何使用代码对ArrayList进行排序,方法是将分数从高到低排列,就像我以前的ArrayList一样,只有@tenfour04的分数,与您已经使用的代码相同,但以新列表的名称进行交换<代码>Collections.sort(scoresList,Collections.reverseOrder())public class ScoreEntry implements Comparable<ScoreEntry> { public final String name; public final int score; public final long time; public ScoreEntry (String name, int score, long time){ this.name = name; this.score = score; this.time = time; } public int compareTo (ScoreEntry other){ if (score == other.score) return Long.signum(other.time - time); return Integer.signum(score - other.score); } }
Comparator<ScoreEntry> nameComparator = new Comparator<>(){
    public int compare(ScoreEntry first, ScoreEntry second) {
        int res = String.CASE_INSENSITIVE_ORDER.compare(first.name, second.name);
        if (res == 0)
            res = first.name.compareTo(second.name);
        return res;
    }
}
Collections.sort(scoresList, nameComparator);