提问者:小点点

在java中创建播放列表的随机播放方法


尽管下面的说明似乎很清楚,但我对如何实现这段代码感到非常困惑。

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

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(sons. size())会给你一个随机索引。请记住,每次将随机选择的歌曲添加到newList时,歌曲的大小都会减少一。每次生成随机数时,您都需要考虑到这一点。

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

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

共3个答案

匿名用户

你在正确的轨道上,但是你忘记了实现描述的一个步骤:

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

匿名用户

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中混洗。

匿名用户

程序崩溃,这是因为,在你的shuffle方法中,while(sons. size()

如果你想用自己的方法编写一个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.
         }
     }

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

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