【问题标题】:How to add in a bests scores history activity, the name of the player who did a best score?如何在最好成绩历史活动中添加成绩最好的球员的名字?
【发布时间】:2020-02-01 00:49:15
【问题描述】:

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

我所拥有的图片:https://i.imgur.com/MX9qUP2.png

还有一张我正在寻找的图片:https://i.imgur.com/6PHHsIt.png

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

我的 2 个数组列表:

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));
        }
    }
}

谢谢

【问题讨论】:

  • 写这样的东西不是那么简单:first.setText("Best score is : " + scoresList.get(0) + "-" + namesList .get(0)); 吗?您也可以使用 ListView(或 RecyclerView)和适配器来完成这项工作
  • 但是 nameList.get(0) 与 scoreList.get(0) 不匹配,因为有一种排序允许我访问 scoreList @Shermano 的最大值
  • 为什么你有一个nameList 和一个scoreList,而你可以有一个playerList 和一个class Player { private String name; private long score; },然后创建一个Comparator&lt;Player&gt; 来按分值对它们进行排序?跨度>

标签: java android android-studio arraylist collections


【解决方案1】:

创建一个类来表示分数/名称条目:

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);
    }
}

然后你可以把它们放在一个 ArrayList 中。通过像这样实现 Comparable,您允许列表按分数排序。

您可能还希望在此类中包含一个日期,以便较早日期的分数高于另一个具有相同分数的条目。您可以使用System.nanoTime() 来获取得分时的时间。

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);
    }
}

编辑:如果你想通过其他方式排序,你需要一个自定义的比较器。我从 this answer 改编而来,其中考虑了大小写。

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;
    }
}

然后你将它传递给 sort 方法:

Collections.sort(scoresList, nameComparator);

【讨论】:

  • 在我的 ArrayList 中,添加 ScoreEntry 值后如何访问分数或名称?和 .add(new ScoreEntry(....));是增加价值的好方法吗?谢谢(对不起,我是新开发的)
  • 这是添加它们的好方法。获取第一个:例如list.get(0).score。
  • 我刚刚找到了方法,但非常感谢,很抱歉,你能告诉我用代码对 ArrayList 进行排序,就像在我的旧 ArrayList 中一样,将分数从高到低排序得分@Tenfour04
  • 您已经在使用相同的代码,但换入新列表的名称。 Collections.sort(scoresList, Collections.reverseOrder());
  • 再次感谢,最后一个问题,是否可以按照玩家姓名的字母顺序(从 a 到 z)对同一个 ArrayList 进行排序?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 2013-02-10
  • 2021-05-09
  • 2020-02-20
  • 2018-10-18
相关资源
最近更新 更多