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

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

JAVA 檢索 arraylist 的獲勝者

JAVA 檢索 arraylist 的獲勝者

明月笑刀無情 2023-07-19 10:55:38
好吧,我正在創(chuàng)建一個(gè)小游戲,我必須從數(shù)組列表中檢索獲勝者。private static ArrayList<Person> setWinner(ArrayList<Person> pers){    for(int i = 0; i < pers.size(); i++)    {        if(pers.get(i).getPoints() //Stuck here...    }    return pers; }如何查看哪些玩家得分最高?
查看完整描述

6 回答

?
largeQ

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

像這樣的東西


if (players == null || players.size() == 0) {

 throw new RuntimeException("You had no players!!");

}



Player winner = players.get(0);


for (Player p: players) {

 if (p.getScore() > winner.getScore) {

  winner = p;

 }

}


winner; // here you got player with the most score.


查看完整回答
反對(duì) 回復(fù) 2023-07-19
?
慕尼黑的夜晚無繁華

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

由于出于某種原因您獲得了某種多個(gè)獲勝者,我認(rèn)為獲勝者是比某些寶藏更多積分的玩家,并且可能只有固定數(shù)量的此類玩家(例如第 1、第 2、第 3 名)。


    int winningScoreTreashold=5///somethis;

            int winningPlayersCount=3;

            List<Player> winners = players.stream()

                    .sorted(Comparator.comparing(Player::getScore))

                    .filter(p -> p.getScore() > winningScoreTreashold)

                    .limit(winningPlayersCount)

                    .collect(Collectors.toList());


   winners//here you got winners;

刪除任何不符合您需求的條件。


查看完整回答
反對(duì) 回復(fù) 2023-07-19
?
慕慕森

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

像這樣的事情可以工作:


import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.stream.Collectors;


class Main {

    public static void main(String[] args) {

        List<Player> players = new ArrayList<>();

        players.add(new Player("Adam", 1));

        players.add(new Player("Bob"));

        players.add(new Player("Cameron", 2));

        players.add(new Player("Daniel", 2));

        System.out.printf("Players: %s%n", players);

        players.sort(Comparator.comparing(Player::getPoints));

        System.out.printf("Sorted Players: %s%n", players);

        List<Player> winners = getWinners(players);

        System.out.printf("Winners: %s%n", winners);

    }


    public static List<Player> getWinners(List<Player> players) {

        int maxPoints = Collections.max(players, Comparator.comparing(Player::getPoints)).getPoints();

        return players.stream().filter(p -> p.getPoints() == maxPoints).collect(Collectors.toList());

    }

}

假設(shè)你的Player類看起來像這樣:


class Player {

    private String name;

    private int points;


    Player(String name, int points) {

        this.name = name;

        this.points = points;

    }


    Player(String name) {

        this.name = name;

        this.points = 0;

    }


    public void updatePoints(int newPoints) {

        this.points = newPoints;

        return;

    }


    public int getPoints() {

        return this.points;

    }


    public String toString() {

        return String.format("%s: %d", this.name, this.points);

    }

}

輸出:


Players: [Adam: 1, Bob: 0, Cameron: 2, Daniel: 2]

Sorted Players: [Bob: 0, Adam: 1, Cameron: 2, Daniel: 2]

Winners: [Cameron: 2, Daniel: 2]


查看完整回答
反對(duì) 回復(fù) 2023-07-19
?
守著一只汪

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

使用以下解決方案怎么樣Streams:


private static List<Player> getWinners(List<Player> players) {

    // Use TreeMap  with reverse order comparator; the highest point will be the first

    Supplier<Map<Integer, List<Player>>> mapFactory = () -> new TreeMap<>(Comparator.reverseOrder());

    Map<Integer, List<Player>> map = Optional.of(players).orElse(Collections.emptyList()).stream()

                                             .collect(Collectors.groupingBy(Player::getPoints, mapFactory, Collectors.toList()));

    // map.keySet() is sorted desc order all points

    return map.isEmpty() ? Collections.emptyList() : map.get(map.keySet().iterator().next());

}


查看完整回答
反對(duì) 回復(fù) 2023-07-19
?
MYYA

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

  1. 您需要首先對(duì)玩家數(shù)組進(jìn)行排序。

  2. 返回與第一項(xiàng)的分?jǐn)?shù)匹配的所有內(nèi)容。

private static ArrayList<Player> getWinners(ArrayList<Player> players) {

        if (players.isEmpty()) {

            throw new RuntimeException("players list is empty");

        }


        // sort the array (desending)

        Comparator<Player> comparator = (Player t, Player t1) -> t1.getPoints() - t.getPoints();

        Collections.sort(players, comparator);


        // filter the array

        Predicate<Player> prdct = (Player t) -> t.score == players.get(0).getPoints();

        List<Player> winners = players.stream().filter(prdct).collect(Collectors.toList());


        return new ArrayList<>(winners);

    }

希望這可以幫助!


查看完整回答
反對(duì) 回復(fù) 2023-07-19
?
翻閱古今

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

private static ArrayList<Player> getWinners(ArrayList<Player> players)

{

    ArrayList<Player> l = new ArrayList<Player>();

    int maxPoints = -1; //Assuming it is not possible to have negative points


    for (int i = 0; i < players.size(); i++)

    {

        int points = players.get(i).getPoints();

        if (points >= maxPoints) {

            if (points > maxPoints) {

                l.clear(); //Clear the return list, since a new "best score" was found

            }

            maxPoints = points;

            l.add(players.get(i));

        }

    }

    return l; 

}


查看完整回答
反對(duì) 回復(fù) 2023-07-19
  • 6 回答
  • 0 關(guān)注
  • 215 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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