【问题标题】:Spitting out the Most Played Song(s) from a List从列表中吐出播放次数最多的歌曲
【发布时间】:2012-12-03 10:54:20
【问题描述】:

您将如何编写代码让用户比较列表中的每首歌曲并返回播放次数最多的歌曲?另外,如果播放次数最多的歌曲不止一首,如何全部显示?

这是我目前所拥有的:

case 5:
        boolean max = false;
        int mostPlays = 1;
        song = new Song(artist,title);

        for(int i = 0; i < songCollection.size(); i++)
        {
        song = songCollection.get(mostPlays);
        if (songCollection.get(i).getNoPlays() < song.getNoPlays())
        {
        songCollection.get(mostPlays).getTitle();
        mostPlays++;
        max = true;
        }
    else
        {
        mostPlays++;
        }
    }

    while (max)
        {
        System.out.println(song.getTitle());
        }
break;

【问题讨论】:

    标签: java arrays for-loop arraylist while-loop


    【解决方案1】:
    1. 按播放次数降序对列表进行排序。
    2. 从头开始,显示歌曲,保存播放次数。
    3. 转到下一首,如果播放次数相同则显示,否则停止。重复。

    相关文档:

    Collections.sort()

    Comparator(用于排序)

    【讨论】:

      猜你喜欢
      • 2012-05-15
      • 2016-07-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      相关资源
      最近更新 更多