JAVA 检索 arraylist 的获胜者

好吧,我正在创建一个小游戏,我必须从数组列表中检索获胜者。


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; 

}

如何查看哪些玩家得分最高?


明月笑刀无情
浏览 143回答 6
6回答

largeQ

像这样的东西if (players == null || players.size() == 0) {&nbsp;throw new RuntimeException("You had no players!!");}Player winner = players.get(0);for (Player p: players) {&nbsp;if (p.getScore() > winner.getScore) {&nbsp; winner = p;&nbsp;}}winner; // here you got player with the most score.

慕尼黑的夜晚无繁华

由于出于某种原因您获得了某种多个获胜者,我认为获胜者是比某些宝藏更多积分的玩家,并且可能只有固定数量的此类玩家(例如第 1、第 2、第 3 名)。&nbsp; &nbsp; int winningScoreTreashold=5///somethis;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int winningPlayersCount=3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Player> winners = players.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted(Comparator.comparing(Player::getScore))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(p -> p.getScore() > winningScoreTreashold)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limit(winningPlayersCount)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp;winners//here you got winners;删除任何不符合您需求的条件。

慕慕森

像这样的事情可以工作:import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.stream.Collectors;class Main {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List<Player> players = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; players.add(new Player("Adam", 1));&nbsp; &nbsp; &nbsp; &nbsp; players.add(new Player("Bob"));&nbsp; &nbsp; &nbsp; &nbsp; players.add(new Player("Cameron", 2));&nbsp; &nbsp; &nbsp; &nbsp; players.add(new Player("Daniel", 2));&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Players: %s%n", players);&nbsp; &nbsp; &nbsp; &nbsp; players.sort(Comparator.comparing(Player::getPoints));&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Sorted Players: %s%n", players);&nbsp; &nbsp; &nbsp; &nbsp; List<Player> winners = getWinners(players);&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Winners: %s%n", winners);&nbsp; &nbsp; }&nbsp; &nbsp; public static List<Player> getWinners(List<Player> players) {&nbsp; &nbsp; &nbsp; &nbsp; int maxPoints = Collections.max(players, Comparator.comparing(Player::getPoints)).getPoints();&nbsp; &nbsp; &nbsp; &nbsp; return players.stream().filter(p -> p.getPoints() == maxPoints).collect(Collectors.toList());&nbsp; &nbsp; }}假设你的Player类看起来像这样:class Player {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int points;&nbsp; &nbsp; Player(String name, int points) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.points = points;&nbsp; &nbsp; }&nbsp; &nbsp; Player(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.points = 0;&nbsp; &nbsp; }&nbsp; &nbsp; public void updatePoints(int newPoints) {&nbsp; &nbsp; &nbsp; &nbsp; this.points = newPoints;&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; public int getPoints() {&nbsp; &nbsp; &nbsp; &nbsp; return this.points;&nbsp; &nbsp; }&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return String.format("%s: %d", this.name, this.points);&nbsp; &nbsp; }}输出:Players: [Adam: 1, Bob: 0, Cameron: 2, Daniel: 2]Sorted Players: [Bob: 0, Adam: 1, Cameron: 2, Daniel: 2]Winners: [Cameron: 2, Daniel: 2]

守着一只汪

使用以下解决方案怎么样Streams:private static List<Player> getWinners(List<Player> players) {&nbsp; &nbsp; // Use TreeMap&nbsp; with reverse order comparator; the highest point will be the first&nbsp; &nbsp; Supplier<Map<Integer, List<Player>>> mapFactory = () -> new TreeMap<>(Comparator.reverseOrder());&nbsp; &nbsp; Map<Integer, List<Player>> map = Optional.of(players).orElse(Collections.emptyList()).stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.groupingBy(Player::getPoints, mapFactory, Collectors.toList()));&nbsp; &nbsp; // map.keySet() is sorted desc order all points&nbsp; &nbsp; return map.isEmpty() ? Collections.emptyList() : map.get(map.keySet().iterator().next());}

MYYA

您需要首先对玩家数组进行排序。返回与第一项的分数匹配的所有内容。private static ArrayList<Player> getWinners(ArrayList<Player> players) {&nbsp; &nbsp; &nbsp; &nbsp; if (players.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException("players list is empty");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // sort the array (desending)&nbsp; &nbsp; &nbsp; &nbsp; Comparator<Player> comparator = (Player t, Player t1) -> t1.getPoints() - t.getPoints();&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(players, comparator);&nbsp; &nbsp; &nbsp; &nbsp; // filter the array&nbsp; &nbsp; &nbsp; &nbsp; Predicate<Player> prdct = (Player t) -> t.score == players.get(0).getPoints();&nbsp; &nbsp; &nbsp; &nbsp; List<Player> winners = players.stream().filter(prdct).collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp; return new ArrayList<>(winners);&nbsp; &nbsp; }希望这可以帮助!

翻阅古今

private static ArrayList<Player> getWinners(ArrayList<Player> players){&nbsp; &nbsp; ArrayList<Player> l = new ArrayList<Player>();&nbsp; &nbsp; int maxPoints = -1; //Assuming it is not possible to have negative points&nbsp; &nbsp; for (int i = 0; i < players.size(); i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int points = players.get(i).getPoints();&nbsp; &nbsp; &nbsp; &nbsp; if (points >= maxPoints) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (points > maxPoints) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l.clear(); //Clear the return list, since a new "best score" was found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxPoints = points;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l.add(players.get(i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return l;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java