Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 高效的日程管理器_Java_Algorithm_Arraylist_Scheduler - Fatal编程技术网

Java 高效的日程管理器

Java 高效的日程管理器,java,algorithm,arraylist,scheduler,Java,Algorithm,Arraylist,Scheduler,所以我现在正在做一个实践项目,我被卡住了 该计划应在一天中列出一系列事件(包括开始时间、结束时间和持续时间),然后找出最有效的时间表(意味着在事件中花费的时间最大化) 我的尝试目前有一个方法,它获取列表,然后从最早的开始时间到最晚的开始时间排序。如果事件的开始时间与另一个事件相同,则会比较较长事件的持续时间,并将该事件放在较早的位置 然后,它应该获取该列表,并基本上一直将它们插入arraylist,直到它重叠为止。如果它重叠,它应该分为两个单独的计划(一个是重叠的事件,另一个是重叠的事件),然后

所以我现在正在做一个实践项目,我被卡住了

该计划应在一天中列出一系列事件(包括开始时间、结束时间和持续时间),然后找出最有效的时间表(意味着在事件中花费的时间最大化)

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

然后,它应该获取该列表,并基本上一直将它们插入arraylist,直到它重叠为止。如果它重叠,它应该分为两个单独的计划(一个是重叠的事件,另一个是重叠的事件),然后它将继续插入事件,直到列表中没有更多的事件

在这之后,它将采取所有的时间表和比较的总持续时间在一个事件中,找到最大的总持续时间,并打印出该时间表

问题- 当我将这些计划插入一个计划arraylist时,它们会不断更新,最终我得到了一个包含所有相同计划的arraylist,而这与创建这个arraylist的目的背道而驰。我读到这是由于arraylist的性质和对象的读取方式所以现在我想知道我如何能有一个列表,我可以一直插入时间表,然后使用该列表来比较所有的持续时间?

代码:

ArrayList list1=new ArrayList();//无组织的活动清单
ArrayList newList=新的ArrayList();
ArrayList schedules=新建ArrayList();//将保留时间表列表
列表1=组织事件列表(列表);
新增(0)//使用列表1的第一项初始化新列表
附表。添加(新列表);
对于(int schedulePos=0;schedulePos最长持续时间){
最长持续时间=持续时间。获取(x);
schedulesPos=x;
}
else if(durations.get(x)=最长持续时间){
System.out.println(“找到了具有相同时间效率的计划”);
}
}
ArrayList finishedList=新的ArrayList();
for(int x=0;x

如果这看起来很混乱,我很抱歉,我对编程还是相当陌生的newList。所有引用都指向同一个对象,您只需从同一列表中删除即可。如果你想有重复的列表,你应该创建列表的副本。你的问题被称为NP完全问题,因此没有有效的解决方案,但也许你研究一下这个问题来编写正确的解决方案。更准确地说,没有算法可以保证在
O(N^C)中找到最优解
某些常数C的时间。有一些启发式算法可以始终找到合适的解决方案,并且一个算法可能会找到某些问题的最优解决方案。这也意味着,如果N足够小,则为大小为N的问题找到最优解在计算上是可行的。换句话说,NP完全并不意味着“放弃”。
    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()));
    }

}