【问题标题】:How to shuffle the contents of an array如何打乱数组的内容
【发布时间】:2014-04-14 07:17:06
【问题描述】:

所以我的程序应该访问一个文本文档,然后执行所有当前有效的爵士乐。我无法弄清楚的唯一问题是如何打乱数组的内容而不让它们最终彼此重叠。互联网和随机循环和 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; 

}

}

【问题讨论】:

  • 如何随机读取数据(索引),而不是随机排列数组?
  • 我该怎么做呢? @parsaporahmad
  • 如何使用 ArrayList stackoverflow.com/a/716619/2399024
  • 注意:在 Java 7 中,您可以使用 Files.readAllLines(),这将从输入文件中读取所有行并将它们全部放入 List&lt;String&gt;
  • 几个答案建议使用集合,确实有一个现成的洗牌,但它只是在幕后使用 Fisher-Yates,如果您已经将数据放在大批。另一方面,在 Java Collections API 中使用经过测试的版本意味着您可能会出错。

标签: java arrays random shuffle


【解决方案1】:
String[] names = ...;
Collections.shuffle(Arrays.asList(names));
// done

请注意,Arrays.asList() 返回由数组支持的可修改(但长度固定)列表,而不是数组的副本。所以数组会被打乱。

【讨论】:

    【解决方案2】:

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

    示例

    int[] values = {1,2,3,4,5};
    
    List<Integer> valuesList = Arrays.asList(values);
    
    Collections.shuffle(valuesList);
    
    // valuesList is shuffled.
    

    【讨论】:

    • 除了这是java,没有Int之类的东西,asList用原始数组搞砸了很多时间。
    • @trutheality 但是有用于原始intInteger 对象包装类。对原始Lists 很有帮助。
    • @AndrewG 除了它不会自动装箱ints 的数组。你需要给asList一个Integers的数组来得到List&lt;Integer&gt;。见stackoverflow.com/questions/1467913/…
    • @trutheality 我没有说它会自动装箱。但是你说没有代表整数的Java对象,这是不正确的。这个答案仍然不正确,您在第二条评论中的逻辑是正确的。
    • @AndrewG 不,我只是说没有Int 我并不是说没有原始包装器。另外我刚刚注意到数组声明语法是错误的。
    【解决方案3】:

    只需使用 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:半数值算法第 1 版和第 3 版。计算机编程艺术系列,第 2 卷,p。 125.
    2. 费舍尔,罗纳德 A.;耶茨,弗兰克 (1948) [1938]。生物、农业和医学研究的统计表(第 3 版)。伦敦:奥利弗和博伊德。第 26-27 页。
    3. http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
    4. https://stackoverflow.com/a/1520212/636009

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 2014-07-24
      • 1970-01-01
      相关资源
      最近更新 更多