如何随机播放数组的内容

时间:2014-03-10 22:37:30

标签: java arrays random shuffle

所以我的程序应该访问一个文本文档然后做所有当前工作的爵士乐。我无法弄清楚的唯一问题是如何在不让它们最终相互叠加的情况下对数组内容进行混洗。互联网和具有随机和for循环的多次尝试都是无用的。这是我的代码:

import java.io.*;
import java.util.*;
public class lab_6 {
public static void main(String[] args)throws FileNotFoundException {
    Scanner input = new Scanner(System.in); //reads from keyboard
    System.out.println("What is the name of your file. ");
    String name = input.nextLine();
    Scanner reader = new Scanner(new File(name));// Open text file
    System.out.println("how many names are in your array");
    int num = input.nextInt();
    String[] names = new String[num];    
    for (int index = 0; index< names.length; index++)
    {
        names[index] = reader.nextLine();// Gets a line while there is one 
    } 
    System.out.println("\nOriginal List");
    printList(names);
    System.out.println("\nShuffled List");
    shuffle(names);
    printList(names);
    System.out.println("\nSorted List");
    Arrays.sort(names);  // this is a built in method
    printList(names);
    System.out.println("What name are you looking for");
    Scanner input1 = new Scanner(System.in); //reads from keyboard
    String find = input1.nextLine();
    int index = search(names,find);
    if(index == -1)
        System.out.println("The name was not there");
    else
        System.out.println(find+" was found at position "+index);
    System.out.println("The average length of all the names is "+averageLength(names));
}
public static void printList(String[] array) // print the list of names numbered
{
    for (int i=0; i<array.length; i++)
    {
        System.out.println((i+1)+") "+ array[i]);
    }

}
public static void shuffle (String[] array) // mix-up the array
{


}
public static int search(String[] array, String find) 
{   
    for(int i=0; i<array.length; i++) {

        if (array[i].equals(find) ) return i;

    } 
    return -1;
}
public static double averageLength(String[] array) //return the average length of the names 
{       
    int sum=0;
    for (int i=0; i<array.length; i++)
    {
        int l= array[i].length();
        sum +=l;
    }
    int average = sum/(array.length);
    return average; 

}

}

3 个答案:

答案 0 :(得分:4)

String[] names = ...;
Collections.shuffle(Arrays.asList(names));
// done

请注意Arrays.asList()返回一个由数组支持的可修改(但固定长度)的列表,而不是数组的副本。因此阵列将被改组。

答案 1 :(得分:2)

只需使用Fisher-Yates shuffle(Knuth算法P):

private Random rand = new Random();

public static void shuffle(String[] array) { // mix-up the array
    for (int i = array.length - 1; i > 0; --i) {
        int j = rand.nextInt(i + 1);
        String temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

请参阅:

  1. Knuth,D。1969,1998:Seminumerical Algorithms 1st&amp;第三版。计算机编程艺术系列,第2卷,p。 125。
  2. Fisher,Ronald A。;耶茨,弗兰克(1948年)[1938]。生物,农业和医学研究统计表(第3版)。伦敦:Oliver&amp;博伊德。第26-27页。
  3. http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
  4. https://stackoverflow.com/a/1520212/636009

答案 2 :(得分:1)

您可以使用Collections类和shuffle方法。该文档可用here

实施例

int[] values = {1,2,3,4,5};

List<Integer> valuesList = Arrays.asList(values);

Collections.shuffle(valuesList);

// valuesList is shuffled.