高效的计划组织者

时间:2017-04-26 11:06:59

标签: java algorithm arraylist scheduler

所以我现在正在做一个练习项目而且我被困住了。

该程序应该记录一天中的事件列表(具有开始时间,结束时间和持续时间),然后找到最有效的时间表(意味着在事件中花费的时间最大化)

我的尝试目前有一个获取列表的方法,然后从最早的开始时间到最晚的顺序进行排序。如果事件的开始时间与另一个事件相同,那么它将比较较长事件的持续时间并放置较早事件的持续时间。

然后,它应该采用该列表并基本上将它们插入到arraylist中直到它重叠。如果它重叠,它应该分支到2个单独的时间表(1个事件中的一个重叠,1个与另一个事件)然后它将继续插入事件,直到列表中没有更多事件。

之后,它将采用所有计划并比较事件中的总持续时间并找到最大的总持续时间并打印出该计划。

问题 - 当我将时间表插入时间表arraylist时,他们会不断更新,我最终会得到一个具有相同时间表的arraylist,这会使得这个arraylist失败。我读到这是由于arraylist的性质以及如何读取对象。 所以现在我想知道我如何有一个列表,我可以继续插入日程表然后使用该列表来比较所有持续时间?

代码:

    ArrayList<Event> list1 = new ArrayList<Event>(); // Unorganized list of events
    ArrayList<Integer> newList = new ArrayList<Integer>();
    ArrayList<ArrayList<Integer>> schedules = new ArrayList<ArrayList<Integer>>(); // will hold List of schedules

    list1 = organizeEventList(list); 
    newList.add(0); //initialize newList with first item of list1
    schedules.add(newList);

    for (int schedulePos = 0; schedulePos < schedules.size(); schedulePos++ ){
        newList = schedules.get(schedulePos);

        for (int x = newList.get(newList.size()-1); x < list1.size()-1; x++){

            int listPos = newList.get(newList.size()-1); // get the list position of the last item of newList
            int key = listPos +1;

            boolean overlap = checkForOverlap(list1.get(listPos).getStartTime(), list1.get(listPos).getEndTime(), list1.get(key).getStartTime(), list1.get(key).getEndTime()); 

            //overlap is true. Duplicate the list and have 1 schedule with event of key and 1 with event of x
            if (overlap == true){
                schedules.add(newList); 
                newList.remove(newList.size()-1);
                newList.add(key);
                schedules.add(schedules.size(), newList);
            }

            else {
                newList.add(key);
                schedules.set((schedules.size()-1), newList);
            }

            newList = schedules.get(schedulePos);
            key++;
        }

    }

    /*
     * Goal: take durations of all schedules in the schedules list and find the most efficient one
     * How? Starting with the schedule in the first spot. Use all their integers to find their durations from list1 and add them to a integer variable. 
     * Store the finished duration to durations arraylist
     * For loop again to find the longest duration from the duration arraylist
     * Take that arraylist from schedules and set newList to that schedule and print it out
     */
    ArrayList<Integer> durations = new ArrayList<Integer>();
    int duration = 0;

    for (int x = 0; x < schedules.size(); x++){

        for (int x1 = 0; x1 < schedules.get(x1).size()-1; x1++){
            duration += list1.get(schedules.get(x).get(x1)).getDuration();
        }

        durations.add(duration);

    }

    int schedulesPos = 0;

    for (int x = 0; x < durations.size(); x++){
        int longestDuration = 0;
        if (durations.get(x) > longestDuration){
            longestDuration = durations.get(x);
            schedulesPos = x;
        }

        else if (durations.get(x) == longestDuration){
            System.out.println("Schedule with the same time efficiency was found." );
        }

    }
    ArrayList<Event> finishedList = new ArrayList<Event>();

    for (int x = 0; x < schedules.get(schedulesPos).size(); x++){
        finishedList.add(list1.get(schedules.get(schedulesPos).get(x)));
    }


    for (int i = 0; i < finishedList.size(); i++){
        System.out.printf("[%s] %s - %s \n", finishedList.get(i).getTitle(), convertIntToTime(finishedList.get(i).getStartTime()), convertIntToTime(finishedList.get(i).getEndTime()));
    }

}

我很抱歉,如果这看起来很混乱,我仍然是编程的新手。

0 个答案:

没有答案
相关问题