0

I intended to create a program which randomly select students and group them into 2 different team. Currently i manage to create output that displays random list but how do i group this output list into 2 different group

public class Random {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

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

        aListColors.add("Red");
        aListColors.add("Green");
        aListColors.add("Blue");
        aListColors.add("Red");
        aListColors.add("Yellow");

        //shuffle the list and get elements from it
        Collections.shuffle( aListColors );

        for(String color : aListColors)

        System.out.println(color);

    }

After Based on comments i have modified my code to this

public static void main(String[] args) {
    // TODO Auto-generated method stub

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

    list.add("Red");
    list.add("Green");
    list.add("Blue");
    list.add("Blue1");
    list.add("Blue2");
    list.add("Blue3");


    //shuffle the list and get elements from it

    for(String color :list) {

        Collections.shuffle(list);
        List[] lists = split(list);         
        System.out.println(lists[0]); 


    }

}

private static List[] split(List<String> list) {    

     List<String> first = new ArrayList<String>(); 
     List<String> second = new ArrayList<String>();

     int size = list.size(); 

     for (int i = 0; i < size / 2; i++) 
            first.add(list.get(i)); 

     for (int i = size / 2; i < size; i++) 
            second.add(list.get(i)); 

     return new List[] { first, second };   

}

but i keep getting multiple values and duplicated values...How to resolve it this is the output Blue3, Blue, Green Red, Green, Blue Red, Blue1, Blue3 Green, Blue, Blue2 Blue1, Blue, Blue3 Blue3, Blue, Blue2

Azzazel 17
  • 29
  • 5
  • 1
    after shuffle, you just have to spllit the list into two [Java: split a List into two sub-Lists?](https://stackoverflow.com/questions/379551/java-split-a-list-into-two-sub-lists) – sidgate Apr 23 '20 at 16:04
  • Just initialize two other lists and put all elements with indexes less than `aListColors.length/2` to the first and others to the second – I.R. Apr 23 '20 at 16:06

0 Answers0