2014-01-23 83 views
-1

我有一个问题找到一种方法来完全随机添加到这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(); 
    } 

} 

谢谢

+0

这不是很清楚。您给了我们一段代码,并且对您想要实现的某些内容进行了模糊的描述。请更具体地说... –

+0

你的意思是不是完全随机的? –

回答

1

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

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

Collections.shuffle(groups); 

其中,Collection是一个Java类..

这是Documentation,提取物是:

公共静态无效的洗牌(名单列表)

随机的置换使用 随机性的默认源指定列表。 所有排列发生的可能性大约相等。

+0

非常感谢! – user2268587