组的随机化

时间:2014-01-23 20:07:48

标签: java random

我在找到一种方法可以完全随机化添加到这4个不同组中的人员时遇到了问题。它现在可以工作,但它不是完全随机的。任何帮助将不胜感激。

我只是改变了它并没有解决它不是完全随机的问题。

public class RandomGroup
{
   /**
    * Arraylist of subgroups in an arraylist of groups 
    * Instance variables fr and sc for file reader and scanner
    */
    private ArrayList<String> subgroup;
    private ArrayList<ArrayList<String>> group;
    public FileReader fr;
    public Scanner sc;


    /**
     * @param subgroup
     * @param group
     */
    public RandomGroup()
    {
    this.subgroup = new ArrayList<String>();
    this.group = new ArrayList<ArrayList<String>>();

    }

    /**
     * Creates a method readAndSopFile uses the scanner class to go through 
     * the file
     * @throws IllegalStateException
     * @throws NoSuchElementException
     */
    public void readAndSopFile() throws IllegalStateException, NoSuchElementException
    {
    while (sc.hasNext())
    {
        String s = sc.next();
        System.out.println(s);
    }
    }
    /**
     * Creates the method groupAndSubgroup
     * @throws IllegalStateException
     * @throws NoSuchElementException
     * @throws FileNotFoundException
     * Creates a fileReader and a scanner to read the file "Students"
     */
    public void groupAndSubgroup() throws IllegalStateException,    NoSuchElementException, FileNotFoundException
    {
    FileReader fr = new FileReader("Students.txt");
    //fr = new FileReader("Students.txt");
    Scanner sc = new Scanner(fr);
    /**
     * Creates another array group from subgroup and adds elements from the
     * text file to the subgroup.
     * Then adds the subgroup to group 0.
     */
    this.subgroup = new ArrayList<String>();
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    group.add(0, subgroup);

    /**
     * Creates another array group from subgroup and adds elements from the
     * text file to the subgroup.
     * Then adds the subgroup to group 1.
     */
    this.subgroup = new ArrayList<String>();
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    group.add(1, subgroup);

    /**
     * Creates another array group from subgroup and adds elements from the
     * text file to the subgroup.
     * Then adds the subgroup to group 2.
     */
    this.subgroup = new ArrayList<String>();
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    group.add(2, subgroup);

    /**
     * Creates another array group from subgroup and adds elements from the
     * text file to the subgroup.
     * Then adds the subgroup to group 3.
     */
    this.subgroup = new ArrayList<String>();
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    subgroup.add(sc.next());
    group.add(3, subgroup);
    /**
     * Executes the method SOP
     */
    this.SOP();
    /**
     * Closes the scanner
     */
    sc.close();
    }

    /**
     * This method shuffles the group array list and outputs the group numbers
     * with the group names till all are in groups.
     */
    public void SOP()
    {

       Collections.shuffle(group);
       for (int i = 0; i < group.size(); i++)
       {
        System.out.println("Group #" + (i+1));
        for (int j = 0; j < group.get(i).size(); j++)
        {
            System.out.println(group.get(i).get(j));
        }
        System.out.println();
       }
    }

    public static void main(String[] args) throws FileNotFoundException
    {
    /**
     * Executes the method groupAndSubgroup
     */
    RandomGroup rg = new RandomGroup();
    rg.groupAndSubgroup();
    }

}

谢谢

1 个答案:

答案 0 :(得分:1)

如果我了解你想做什么,你可以试试这个:

List<String> groups = new ArrayList<String>();

Collections.shuffle(groups);

其中,Collection是一个java类..

这是Documentation,摘录是:

  

public static void shuffle(List list)

     

使用默认来源随机置换指定列表   随机性。 所有排列都大致相等   可能性。