首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将ArrayList中的字符串转换和输出计数转换为Asterisk

将ArrayList中的字符串转换和输出计数转换为Asterisk
EN

Stack Overflow用户
提问于 2019-03-12 08:28:16
回答 1查看 0关注 0票数 0

我的方法中有一个棒球运动员列表populateBaseballPlayers,但是不是以方法中的数字形式显示棒球运动员的数量displayFavoriteBaseballPlayersData,我怎样才能显示计数asterisks *?输出应该是Mike Trout : *** Mookie Betts : *****等等。

我知道这可能超级简单,但我迷失了。

这是我的完整代码:

FavoriteBaseballPlayers 类:

import java.util.ArrayList;
public class FavoriteBaseballPlayers
{

    private ArrayList<String> favoriteBaseballPlayers;

    /**
     * Constructor to populate the list of baseball favorite baseball players
     */
    public FavoriteBaseballPlayers()
    {
        favoriteBaseballPlayers = new ArrayList<>();
        populateBaseballPlayers();
    }

    /**
     * Pre-set the list of favorite baseball players
     */
    private void populateBaseballPlayers()
    {
        favoriteBaseballPlayers.add("Bryce Harper");
        favoriteBaseballPlayers.add("Aaron Judge");
        favoriteBaseballPlayers.add("Clayton Kershaw");
        favoriteBaseballPlayers.add("Mike Trout");
        favoriteBaseballPlayers.add("Mike Trout");
        favoriteBaseballPlayers.add("Bryce Harper");
        favoriteBaseballPlayers.add("Mookie Betts");
        favoriteBaseballPlayers.add("Francisco Lindor");
        favoriteBaseballPlayers.add("Mookie Betts");
        favoriteBaseballPlayers.add("Mookie Betts");
        favoriteBaseballPlayers.add("Clayton Kershaw");
        favoriteBaseballPlayers.add("Gerrit Cole");
        favoriteBaseballPlayers.add("JD Martinez");
    }

    /**
     * Get the list of favorite baseball players
     * @return the baseball players in the ArrayList
     */
    public ArrayList<String> getFavoriteBaseballPlayers()
    {
        return favoriteBaseballPlayers;
    }
}

这是FavoriteBaseballPlayersDisplay类:

    import java.util.ArrayList;
    public class FavoriteBaseballPlayersDisplay
    {
        private ArrayList<String> baseballPlayers;
        private int[] countOfFavoriteBaseballPlayers;

        /**
         * Constructor for the objects fo the FavoriteBaseballPlayersDisplay class
         */
        public FavoriteBaseballPlayersDisplay()
        {
            FavoriteBaseballPlayers players = new FavoriteBaseballPlayers();
            baseballPlayers = players.getFavoriteBaseballPlayers();
            scanBaseballPlayerData();

        }

    void printPlayerCount(final String player, final int count)
    {
    System.out.print(player + ":\t");

    for (int i = 0; i < count; i++) {
        System.out.print("*");
    }

    System.out.println();
    }

        /**
         * Examine the baseball player data and document the counts of the baseball
         * players
         */
        private void scanBaseballPlayerData()
        {
            countOfFavoriteBaseballPlayers = new int[6];
            for (String player : baseballPlayers) {
                if (player.equals("Aaron Judge")) {
                    countOfFavoriteBaseballPlayers[0]++;
                }
                else if (player.equals("Bryce Harper")) {
                    countOfFavoriteBaseballPlayers[1]++;
                }
                else if (player.equals("Mookie Betts")) {
                    countOfFavoriteBaseballPlayers[2]++;
                }
                else if (player.equals("Mike Trout")) {
                    countOfFavoriteBaseballPlayers[3]++;
                }
                else if (player.equals("Clayton Kershaw")) {
                    countOfFavoriteBaseballPlayers[4]++;
                }
                else if (player.equals("Francisco Lindor")) {
                    countOfFavoriteBaseballPlayers[5]++;
                }
                else {
                    countOfFavoriteBaseballPlayers[6]++;
                }
        }
        }

/**
 * Display the baseball player data in the form of a histogram
 */
public void displayFavoriteBaseballPlayersData()
{
    System.out.println("Here are the count of favorite baseball players that were selected as favorites:");
    printPlayerCount("Aaron Judge: " + countOfFavoriteBaseballPlayers[0]);
    printPlayerCount("Bryce Harper: " + countOfFavoriteBaseballPlayers[1]);
    printPlayerCount("Mookie Betts: " + countOfFavoriteBaseballPlayers[2]);
    printPlayerCount("Mike Trout: " + countOfFavoriteBaseballPlayers[3]);
    printPlayerCount("Clayton Kershaw: " + countOfFavoriteBaseballPlayers[4]);
    printPlayerCount("Francisco Lindor: " + countOfFavoriteBaseballPlayers[5]);
    printPlayerCount("Other: " + countOfFavoriteBaseballPlayers[6]);
}      
}
EN

回答 1

Stack Overflow用户

发布于 2019-03-12 18:25:23

无论如何,我建议你选择一个更简单的路径。而不是维护一个单独的arraycountOfFavoriteBaseballPlayers),使用一个Stream。您可以玩家分组收集到计数。

final Map<String, Long> count =
                baseballPlayers.stream()
                               .collect(Collectors.groupingBy(
                                       Function.identity(),
                                       Collectors.counting()));

Function.identity()等于lambda s -> s,它基本上返回String

然后,您可能会有另一个方法接受播放器名称和出现次数

printPlayerCount("Bryce Harper", count.get("Bryce Harper"));
printPlayerCount("Aaron Judge", count.get("Aaron Judge"));
printPlayerCount("Clayton Kershaw", count.get("Clayton Kershaw"));
printPlayerCount("Mike Trout", count.get("Mike Trout"));

void printPlayerCount(final String player, final Long count) {
    System.out.print(player + ":\t");

    for (int i = 0; i < count; i++) {
        System.out.print("*");
    }

    System.out.println();
}

这会打印出:

Bryce Harper:       **
Aaron Judge:        *
Clayton Kershaw:    **
Mike Trout:         **

要继续使用单独的array,您还可以考虑提取方法

void printPlayerCount(final String player, final int count) {
    System.out.print(player + ":\t");

    for (int i = 0; i < count; i++) {
        System.out.print("*");
    }

    System.out.println();
}

并使用:

printPlayerCount("Aaron Judge", countOfFavoriteBaseballPlayers[0]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档