在java中为播放列表创建一个shuffle方法

问题描述 投票:0回答:4

虽然下面的说明似乎很清楚,但我对如何实现此代码完全感到困惑。

当代音乐软件最受欢迎的功能之一是能够随机化播放列表中歌曲的顺序 - 一种称为“改组”歌曲的动作。使用以下伪代码作为指南创建一个shuffle方法:

create a new empty arraylist (called newList)

while (there are still songs left)

randomly select the index of one song on the playlist

remove it from the current playlist and place it at the end of newList

songs = newList

提示:使用Java库中的Random类生成随机数。它的方法是:public int nextInt(int n)。这将返回一个伪随机,均匀分布的int值,该值低至0且高至n。因此,nextInt(songs.size())为您提供随机索引。请记住,每次将随机选择的歌曲添加到newList时,歌曲的大小会减1。每次生成随机数时,您都需要考虑到这一点。

这就是我所导致程序崩溃的原因。我需要帮助从数组中检索一首歌,将其删除,并将其放入一个新的数组列表中。请帮我!

public int nextInt(int n) {

int index = randomGenerator.nextInt(songs.size());
              return index;
 }
public void shuffle (){
    newList = new ArrayList<Mp3> ();
    while (songs.size()>0){
        Mp3 song = songs.get(nextInt(songs.size()));
        newList.add(song);
        System.out.println("new list" + newList);
   }
 }
java arrays random
4个回答
1
投票

你在那里正确的轨道,但你忘了实施描述的一个步骤:

remove it from the current playlist and place it at the end of newList

需要将Shuffle方法重写为以下内容:

public void shuffle (){
    newList = new ArrayList<Mp3> ();
    while (songs.size()>0){
        Mp3 song = songs.get(nextInt(songs.size()));
        songs.remove(song); // the forgotten step
        newList.add(song);
        System.out.println("new list" + newList);
   }
 }

0
投票

程序崩溃,这是因为,在你的shuffle方法中,while (songs.size()>0){总是true。列表的大小不会改变。

如果你想用自己的方法编写shuffle方法,那么一种简单的方法是迭代歌曲列表并用随机索引交换当前索引i和歌曲的2首歌曲。

     public void shuffle (List<Mp3> songsList)
     {
         for(int i=0;i< songsList.size(); i++)
         {
             //Do something here
                              //generate a random number
             //Swap songs according to the i index and and random index.
         }
     }

最简单的方法是使用Collections #shuffle方法使列表随机。

Collections中相应的shuffle源代码如下:

/**
 * Randomly permutes the specified list using a default source of
 * randomness.  All permutations occur with approximately equal
 * likelihood.<p>
 *
 * The hedge "approximately" is used in the foregoing description because
 * default source of randomness is only approximately an unbiased source
 * of independently chosen bits. If it were a perfect source of randomly
 * chosen bits, then the algorithm would choose permutations with perfect
 * uniformity.<p>
 *
 * This implementation traverses the list backwards, from the last element
 * up to the second, repeatedly swapping a randomly selected element into
 * the "current position".  Elements are randomly selected from the
 * portion of the list that runs from the first element to the current
 * position, inclusive.<p>
 *
 * This method runs in linear time.  If the specified list does not
 * implement the {@link RandomAccess} interface and is large, this
 * implementation dumps the specified list into an array before shuffling
 * it, and dumps the shuffled array back into the list.  This avoids the
 * quadratic behavior that would result from shuffling a "sequential
 * access" list in place.
 *
 * @param  list the list to be shuffled.
 * @throws UnsupportedOperationException if the specified list or
 *         its list-iterator does not support the <tt>set</tt> method.
 */
public static void shuffle(List<?> list) {
    shuffle(list, r);
}

-1
投票
import java.util.Random;

public class SuffleSongs {

    public static void main(String[] args) {

        List<String> playList = new ArrayList<String>();
        playList.add("Song1");
        playList.add("Song2");
        playList.add("Song3");
        playList.add("Song4");
        playList.add("Song5");
        playList.add("Song6");
        playList.add("Song7");
        playList.add("Song8");
        playList.add("Song9");
        playList.add("Song10");
        playList.add("Song11");
        playList.add("Song12");
        playList.add("Song13");
        playList.add("Song14");
        playList.add("Song15");
        playList.add("Song16");
        playList.add("Song17");
        playList.add("Song18");
        playList.add("Song19");
        playList.add("Song20");
        playList.add("Song21");

        // shuffle the playlist
        for (int i=0; i<playList.size(); ++i) {
            Random rand = new Random();
            int temp = rand.nextInt(playList.size() -i) + i;
            Collections.swap(playList, i, temp);
        }

        // print the shuffled playlist
        for(int j = 0; j < playList.size(); ++j) {
            System.out.println(playList.get(j));
        }

    }

}

这将随机播放而无需创建新的播放列表(ArrayList)。 基本上这个代码只获取播放列表ArrayList,然后在同一个ArrayList中随机播放。

© www.soinside.com 2019 - 2024. All rights reserved.