猿问

如何在最佳成绩历史记录活动中添加取得最佳成绩的玩家的姓名?

我有一个带有测验和活动的应用程序,我希望获得前 5 名得分的玩家。我有 2 个 ArrayList,其中一个存储分数,另一个存储玩家的姓名。我设法用分数列出了清单,但我不知道如何在同行分数旁边列出正确的名字。

我所拥有的图像:https ://i.stack.imgur.com/7PxmR.png

以及我正在寻找的图像:https://i.stack.imgur.com/HNEsU.png

我尝试过使用 Map,但我不知道不能存储 2 个相同的键,所以这是不可能的,但可能有一种方法可以拥有某种带有索引的数组列表,但可以有 2 个不同的信息,我不知道知道。

我的 2 个 ArrayList :

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

我获得 5 个最佳成绩的方法:

public class HistoryActivity extends AppCompatActivity {


    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_history);


        TextView first = (TextView) findViewById(R.id.history_activity_first);

        TextView second = (TextView) findViewById(R.id.history_activity_second);

        TextView third = (TextView) findViewById(R.id.history_activity_third);

        TextView fourth = (TextView) findViewById(R.id.history_activity_fourth);

        TextView fifth = (TextView) findViewById(R.id.history_activity_fifth);


        Collections.sort(scoresList, Collections.reverseOrder());


        first.setText("Best score is : " + scoresList.get(0));


        if (scoresList.size() >= 2) {

            second.setText("2nd best score is : " + scoresList.get(1));

        }


        if (scoresList.size() >= 3) {

            third.setText("3rd best score is : " + scoresList.get(2));

        }


        if (scoresList.size() >= 4) {

            fourth.setText("4th best score is : " + scoresList.get(3));

        }


        if (scoresList.size() >= 5) {

            fifth.setText("5th best score is : " + scoresList.get(4));

        }

    }

}


ibeautiful
浏览 90回答 1
1回答

凤凰求蛊

创建一个类来表示分数/名称条目:public class ScoreEntry implements Comparable<ScoreEntry> {&nbsp; &nbsp; public final String name;&nbsp; &nbsp; public final int score;&nbsp; &nbsp; public ScoreEntry (String name, int score){&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.score = score;&nbsp; &nbsp; }&nbsp; &nbsp; public int compareTo (ScoreEntry other){&nbsp; &nbsp; &nbsp; &nbsp; return Integer.signum(score - other.score);&nbsp; &nbsp; }}然后你可以将它们放入 ArrayList 中。通过像这样实现 Comparable,您可以允许列表按分数排序。您可能还想在此类中包含一个日期,以便较早日期取得的分数排名高于具有相同分数的其他条目。System.nanoTime()当得分时,您可以使用long 来获取时间。public class ScoreEntry implements Comparable<ScoreEntry> {&nbsp; &nbsp; public final String name;&nbsp; &nbsp; public final int score;&nbsp; &nbsp; public final long time;&nbsp; &nbsp; public ScoreEntry (String name, int score, long time){&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.score = score;&nbsp; &nbsp; &nbsp; &nbsp; this.time = time;&nbsp; &nbsp; }&nbsp; &nbsp; public int compareTo (ScoreEntry other){&nbsp; &nbsp; &nbsp; &nbsp; if (score == other.score)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Long.signum(other.time - time);&nbsp; &nbsp; &nbsp; &nbsp; return Integer.signum(score - other.score);&nbsp; &nbsp; }}编辑:如果您想通过其他方式排序,您需要一个自定义比较器。我根据这个答案改编了这个,它考虑了大写。Comparator<ScoreEntry> nameComparator = new Comparator<>(){&nbsp; &nbsp; public int compare(ScoreEntry first, ScoreEntry second) {&nbsp; &nbsp; &nbsp; &nbsp; int res = String.CASE_INSENSITIVE_ORDER.compare(first.name, second.name);&nbsp; &nbsp; &nbsp; &nbsp; if (res == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res = first.name.compareTo(second.name);&nbsp; &nbsp; &nbsp; &nbsp; return res;&nbsp; &nbsp; }}然后你将其传递给排序方法:Collections.sort(scoresList, nameComparator);
随时随地看视频慕课网APP

相关分类

Java
我要回答