java高效日程管理器
所以我现在正在做一个实践项目,我被卡住了
该计划应该列出一天中的活动(包括开始时间、结束时间和持续时间),然后找到最有效的时间表(意味着活动花费的时间最大化)
我的尝试目前有一种方法,可以获取列表,然后从最早的开始时间到最晚的开始时间排序。如果事件的开始时间与另一个事件相同,则会比较较长事件的持续时间,并将其提前放置
然后,它应该获取该列表,基本上一直将它们插入arraylist,直到重叠。如果它重叠,它应该分为两个单独的计划(一个是重叠的事件,另一个是重叠的事件),然后它将继续插入事件,直到列表中没有更多的事件
之后,它将把所有的时间表和比较的总持续时间在一个事件中,找到最大的总持续时间,并打印出该时间表
问题- 当我将这些计划插入一个计划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) 个答案