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

时间:2013-12-10 06:45:30

标签: java arrays random

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

当代音乐软件最受欢迎的功能之一是能够随机化播放列表中歌曲的顺序 - 一种称为“改组”歌曲的动作。使用以下伪代码作为指南创建一个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);
   }
 }

4 个答案:

答案 0 :(得分: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.
         }
     }

最简单的方法是使用集合#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 :(得分:0)

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

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);
   }
 }

答案 2 :(得分:0)

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中随机播放。

答案 3 :(得分:-2)

采用洗牌方法来洗牌。希望它有所帮助

public static int[] shuffleDeck(int[] a,int n) {
        for(int i=0;i<52;i++) {
            /* I have taken a number 53 because it is a prime number and greater than 52
             * that is helpful in avoiding collisions i.e repetitive number.
             */
            a[i] = ((i+1)*n)%53;     
        }
        return a;
    }