01 October 2014

Creating Shufflled Playlist In java

import java.util.ArrayList;

public class PlayList {

    public static void main(String[] args) {
     for (int k = 0; k < 10; k++) {
         ArrayList songs = new ArrayList(10);
         songs.add("one");
         songs.add("two");
         songs.add("three");
         songs.add("four");
         songs.add("five");
         songs.add("six");
         songs.add("seven");
         songs.add("eight");
         songs.add("nine");
         songs.add("ten");
         String shuffled[] = new String[10];
         int i = 0;
         while (true) {
              if (songs.size() > 1) {
                 Double random = Math.random();
                 int index = (int) (random * songs.size());
                 shuffled[i++] = songs.remove(index);
                 continue;
              }
             shuffled[i++] = songs.remove(0);
             break;
         }
         System.out.print(":: ");
         for (int j = 0; j < shuffled.length; j++) {
               System.out.print(shuffled[j] + ", ");
          }
          System.out.println(" ::");
       }
   }
}

No comments:

Post a Comment