为了账号安全,请及时绑定邮箱和手机立即绑定

JAVA 检索 arraylist 的获胜者

JAVA 检索 arraylist 的获胜者

明月笑刀无情 2023-07-19 10:55:38
好吧,我正在创建一个小游戏,我必须从数组列表中检索获胜者。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贡献2039条经验 获得超7个赞

像这样的东西


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.


查看完整回答
反对 回复 2023-07-19
?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

由于出于某种原因您获得了某种多个获胜者,我认为获胜者是比某些宝藏更多积分的玩家,并且可能只有固定数量的此类玩家(例如第 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;

删除任何不符合您需求的条件。


查看完整回答
反对 回复 2023-07-19
?
慕慕森

TA贡献1856条经验 获得超17个赞

像这样的事情可以工作:


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

    }

}

假设你的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]


查看完整回答
反对 回复 2023-07-19
?
守着一只汪

TA贡献1872条经验 获得超3个赞

使用以下解决方案怎么样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());

}


查看完整回答
反对 回复 2023-07-19
?
MYYA

TA贡献1868条经验 获得超4个赞

  1. 您需要首先对玩家数组进行排序。

  2. 返回与第一项的分数匹配的所有内容。

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

    }

希望这可以帮助!


查看完整回答
反对 回复 2023-07-19
?
翻阅古今

TA贡献1780条经验 获得超5个赞

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; 

}


查看完整回答
反对 回复 2023-07-19
  • 6 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信