如何判断你是否已经到了arraylist的for循环结束?

时间:2014-11-04 10:49:27

标签: java arraylist

我正在尝试构建播放列表功能,该功能需要一个音乐库并从中生成一个指定最小持续时间的随机播放列表。这个想法是它需要用户的最低评级 - 但是,如果没有足够的评级来填充播放列表,那么它将用于minimumRating - 1的曲目。因此,如果你想要60分钟的5星级曲目,但是你没有足够的,那么该计划将尝试用4星级曲目来填补其余部分。

所以我需要一种方法,一旦它到达其for循环结束时递减minimumRating,然后再将其关闭。有没有一种方法我可以使用它返回一些东西(可能是一个布尔值),如果for循环到达它的末尾?

如果它有用,我的当前代码可以在这里找到:

public ArrayList<Track> createPlaylist(int minRating, int minDuration) { minDuration = minDuration * 60; ArrayList<Track> shuffledList = trackList; ArrayList<Track> playlist = new ArrayList<Track>(); Collections.shuffle(shuffledList); int playlistDuration = 0; while (playlistDuration < minDuration) { for (Track track : shuffledList) { if (track.getRating() >= minRating) { playlist.add(track); playlistDuration += track.getLength(); } } } return playlist; }

3 个答案:

答案 0 :(得分:1)

您可以这样做:

public ArrayList<Track> createPlaylist(final int minRating, int minDuration) {
    minDuration = minDuration * 60;
    final ArrayList<Track> shuffledList = trackList;
    final ArrayList<Track> playlist = new ArrayList<Track>();
    Collections.shuffle(shuffledList);
    int playlistDuration = 0;
    int myMin = minRating;
    while (playlistDuration < minDuration && myMin > 0) {
        for (final Track track : shuffledList) {
            if (track.getRating() >= myMin && !playlist.contains(track)) {
                playlist.add(track);
                playlistDuration += track.getLength();
            }
        }
        myMin--;
    }
    return playlist;
}

答案 1 :(得分:1)

我想到的第一件事就是你可以为播放列表制作一个课程:

public class Playlist {
    private final List<Track> playlist = new ArrayList<Track>();
    private int playlistDuration = 0;

    public void addTrack(Track track) {
        playlist.add(track);
        playlistDuration += track.getLength();
    }

    public int getDuration() {
        return playlistDuration;
    }

    public List<Track> getPlaylist() {
        return playlist;
    }

    public void shufflePlaylist() {
        Collections.shuffle(playlist);
    }
}

你可以在代码中省去很多行,你的for循环看起来像这样:

public ArrayList<Track> createPlaylist(int minRating, int minDuration) {
    minDuration = minDuration * 60;
    playlist.shufflePlaylist();

    while (playlist.getDuration() < minDuration) {
        for (Track track : trackList) {
            if (track.getRating() >= minRating) {
                playlist.addTrack(track);
            }
            if (playlist.getDuration() > minDuration) {
                return;
            }
        }
    }
    return playlist;
}

答案 2 :(得分:0)

public ArrayList<Track> createPlaylist(int minRating, int minDuration) {
    minDuration = minDuration * 60;
    ArrayList<Track> sortedList = trackList;
    ArrayList<Track> playlist = new ArrayList<Track>(); 
    Collections.sort(sortedList, Collections.reverseOrder()); // implements comparable at rating
    int playlistDuration = 0; 

    for (Track track : sortedList) { 
        playlist.add(track); 
        playlistDuration += track.getLength(); 
        if (playlistDuration > minDuration)
            break;

    } 

    Collections.shuffle(playlist);
    return playlist
}