Makistos
10/28/2013 - 7:56 AM

## This is a round-robin algorithm implemented in Java. #round-robin #scheduling #algorithm #java

This is a round-robin algorithm implemented in Java. #round-robin #scheduling #algorithm #java

``````import java.util.*;

public class Scheduler {
/** The schedule list. */
private Matches matches = new Matches();

public Scheduler(Teams participants) {
createSchedule(participants);
}

/** Creates the entire schedule
*
* @param participants List of participants for whom to create the schedule.
*/
private void createSchedule(Teams participants) {
if (participants.getSize() % 2 == 1) {
// Number of teams uneven ->  add the bye team.
}

ArrayList list = new ArrayList();
// First, lets turn the LinkedHashMap into a list for easier manipulation
for (Iterator itr=pList.values().iterator();itr.hasNext();) {
}

for(int i = 1;i<list.size();i++) {
createOneRound(i, list);
// Move last item to first
list.remove(list.size()-1);
}

}

/** Creates one round, i.e. a set of matches where each team plays once.
*
* @param round Round number.
* @param list List of teams
*/
private void createOneRound(int round, ArrayList teams) {
int mid = teams.size() / 2;
// Split list into two

ArrayList l1 = new ArrayList();
// Can't use sublist (can't cast it to ArrayList - how stupid is that)??
for(int j=0;j<mid;j++) {
}

ArrayList l2 = new ArrayList();
// We need to reverse the other list
for(int j=teams.size()-1;j>=mid;j--) {
}

for(int tId = 0;tId<l1.size();tId++) {
Teams.Data t1;
Teams.Data t2;
// Switch sides after each round
if (round %2 == 1) {
t1 = (Teams.Data)l1.get(tId);
t2 = (Teams.Data)l2.get(tId);
} else {
t1 = (Teams.Data)l2.get(tId);
t2 = (Teams.Data)l1.get(tId);
}