java 这是一种用Java实现的循环算法。 #round-robin #scheduling #algorithm #java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 这是一种用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.
            participants.addBye();
        }

        LinkedHashMap<Integer, Teams.Data> pList= participants.getList();
        ArrayList list = new ArrayList();
        // First, lets turn the LinkedHashMap into a list for easier manipulation
        for (Iterator itr=pList.values().iterator();itr.hasNext();) {
            list.add(itr.next());
        }

        for(int i = 1;i<list.size();i++) {
            createOneRound(i, list);
            // Move last item to first
            list.add(1, list.get(list.size()-1));
            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++) {
            l1.add(teams.get(j));
        }

        ArrayList l2 = new ArrayList();
        // We need to reverse the other list
        for(int j=teams.size()-1;j>=mid;j--) {
            l2.add(teams.get(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);
            }
            matches.addNew(round, ((round-1)*l1.size())+(tId+1), (String)t1.get("name"), (String)t2.get("name"));
        }
    }

    /** Returns the match list (schedule)
     */
    public Matches get() {
        return matches;
    }
}

以上是关于java 这是一种用Java实现的循环算法。 #round-robin #scheduling #algorithm #java的主要内容,如果未能解决你的问题,请参考以下文章

Java集合类学习笔记2

JAVA调用R脚本

经典算法二分查找循环实现Java版

JAVA种用一个类去调用一个接口的多个实现类

Java 数据结构 & 算法宁可累死自己, 也要卷死别人 6 循环队列

Java 数据结构 & 算法宁可累死自己, 也要卷死别人 6 循环队列