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