2013-10-11 89 views
1

我有一個基於它們在他們在指定隊列的順序

我想讓它做什麼的目錄順序播放歌曲,爲了一個類時,我發揮了一定的歌,我想它從隊列中刪除之前所有的歌,使其只播放歌曲以下

這是隊列創建

http://pastebin.com/NwPx2nru

+1

**無關(如一個側面說明):**你的方法名是C/C++風格(使用下劃線到每個單詞的方法的名字分開,而不是* camelCase *)。雖然不重要,但只是一個說明。也許這會很有用:[Java編程語言的代碼約定](http://www.oracle.com/technetwork/java/codeconv-138413.html) – informatik01

回答

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語法