第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在最佳成績(jī)歷史記錄活動(dòng)中添加取得最佳成績(jī)的玩家的姓名?

如何在最佳成績(jī)歷史記錄活動(dòng)中添加取得最佳成績(jī)的玩家的姓名?

ibeautiful 2023-09-20 16:03:03
我有一個(gè)帶有測(cè)驗(yàn)和活動(dòng)的應(yīng)用程序,我希望獲得前 5 名得分的玩家。我有 2 個(gè) ArrayList,其中一個(gè)存儲(chǔ)分?jǐn)?shù),另一個(gè)存儲(chǔ)玩家的姓名。我設(shè)法用分?jǐn)?shù)列出了清單,但我不知道如何在同行分?jǐn)?shù)旁邊列出正確的名字。我所擁有的圖像:https ://i.stack.imgur.com/7PxmR.png以及我正在尋找的圖像:https://i.stack.imgur.com/HNEsU.png我嘗試過(guò)使用 Map,但我不知道不能存儲(chǔ) 2 個(gè)相同的鍵,所以這是不可能的,但可能有一種方法可以擁有某種帶有索引的數(shù)組列表,但可以有 2 個(gè)不同的信息,我不知道知道。我的 2 個(gè) ArrayList :public static ArrayList<Integer> scoresList = new ArrayList<>(); public static ArrayList<String> namesList = new ArrayList<>();我獲得 5 個(gè)最佳成績(jī)的方法: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));        }    }}
查看完整描述

1 回答

?
鳳凰求蠱

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊

創(chuàng)建一個(gè)類(lèi)來(lái)表示分?jǐn)?shù)/名稱(chēng)條目:


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 中。通過(guò)像這樣實(shí)現(xiàn) Comparable,您可以允許列表按分?jǐn)?shù)排序。


您可能還想在此類(lèi)中包含一個(gè)日期,以便較早日期取得的分?jǐn)?shù)排名高于具有相同分?jǐn)?shù)的其他條目。System.nanoTime()當(dāng)?shù)梅謺r(shí),您可以使用long 來(lái)獲取時(shí)間。


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

    }

}

編輯:如果您想通過(guò)其他方式排序,您需要一個(gè)自定義比較器。我根據(jù)這個(gè)答案改編了這個(gè),它考慮了大寫(xiě)。


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


查看完整回答
反對(duì) 回復(fù) 2023-09-20
  • 1 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)