有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

指定队列顺序的java

我有一个类,根据歌曲在目录中的顺序播放歌曲

我想让它做的是,每当我播放某首歌曲时,我希望它从队列中删除它前面的所有歌曲,以便它只播放下面的歌曲

这是创建队列的类

http://pastebin.com/NwPx2nru


共 (1) 个答案

  1. # 1 楼答案

    您可以不断从队列中删除项目,直到找到要播放的特定歌曲,然后停止播放。结果将是一个队列,包含特定歌曲之后的所有项目,而之前没有任何项目

    public void playSpecificSong(String specificSong) {
        String nextSong = songQueue.remove();
        while (!nextSong.equals(specificSong) && !songQueue.isEmpty()) {
            nextSong = songQueue.remove();
        }
    
        if (nextSong.equals(specificSong)) {
            // specific song was found in the queue and is held within the nextSong variable
            // songQueue now contains all songs AFTER specificSong and nothing before
        } else {
            // specific song wasn't found in the queue
            // songQueue is now empty
        }
    }
    

    编辑:将变量更改为Java语法