Java 如何在arraylist中每天标记和存储每个时隙
我每30分钟为你创造一个时间段。但我不知道如何在数组中标记从周一到周五的每个时隙。这是我想要如何标记时间段的示例Java 如何在arraylist中每天标记和存储每个时隙,java,timeslots,Java,Timeslots,我每30分钟为你创造一个时间段。但我不知道如何在数组中标记从周一到周五的每个时隙。这是我想要如何标记时间段的示例 MS1 -> Monday 0800-0830 MS2 -> Monday 0830-0900 . . TS1 -> Tuesday 0800-0830 TS2 -> Tuesday 0830-0900 ... FS16 -> Friday 0530->0600 这就是我在timeslot类中创建时间段的方式 //getter for ho
MS1 -> Monday 0800-0830
MS2 -> Monday 0830-0900
.
.
TS1 -> Tuesday 0800-0830
TS2 -> Tuesday 0830-0900
...
FS16 -> Friday 0530->0600
这就是我在timeslot类中创建时间段的方式
//getter for hour
public int getHour(){return hour;}
//param hour the hour to set
public void setHour(int hour){
//24 hours
if((hour >= 0) && (hour < 24)){
this.hour = hour;}}
//getter for minute
public int getMinute(){return minute;}
//param minute the minute to set
public void setMinute(int minute){
if(minute >0){
this.hour += minute/60;
this.minute = 30 * ((minute % 60) / 30);}
else{
minute = 0;}}
您可以按一周中的天数使用2d数组排序来收集数据。大概是这样的:
List<List<TimeSlot>> timeSlotsSortedByDay = new ArrayList<>();
List<TimeSlot> sundayTimeSlots = new ArrayList<>();
//Add all the time slots for sunday
sundayTimeSlots.add(new TimeSlot(30));
...
//Add sunday to main list
timeSlotsSortedByDay.add(sundayTimeSlots);
//Do the rest of the days using the same idea.
...
//Access time slots for a given day
timeSlotsSortedByDay.get(0); //All time slots for sunday
timeSlotsSortedByDay.get(1); //All time slots for monday
是
Date
类型的java.util.Date
还是您创建的自定义类?在前一种情况下,代码不会编译,因为java.util.Date
没有您正在使用的构造函数。如果您使用的是自定义类,那么可能应该包括它的详细信息。此外,你到底愿意实现什么?Map
可能比array更适合您,但我需要更具体的细节来确定。Date类是一个自定义类。date类用于设置日期和时间(每30分钟一次)。我正在尝试使用遗传算法为我所在大学的排课系统创建时间段。我不知道如何为每天的每个时段贴上标签并存储时间段。没关系。只是对它如何每30分钟存储一次感到困惑。@Noren它存储你给它的时间段。如果您想每30分钟创建一个时隙
,并添加它,您只需为
创建一个循环,以30分钟为间隔循环。
mondayTimeSlot.add(new TimeSlot(0));
mondayTimeSlot.add(new TimeSlot(1));
....
fridayTimeSlot.add(new TimeSlot(15));
List<List<TimeSlot>> timeSlotsSortedByDay = new ArrayList<>();
List<TimeSlot> sundayTimeSlots = new ArrayList<>();
//Add all the time slots for sunday
sundayTimeSlots.add(new TimeSlot(30));
...
//Add sunday to main list
timeSlotsSortedByDay.add(sundayTimeSlots);
//Do the rest of the days using the same idea.
...
//Access time slots for a given day
timeSlotsSortedByDay.get(0); //All time slots for sunday
timeSlotsSortedByDay.get(1); //All time slots for monday
//Example if you want to iterate over all timeSlots for monday
for (int i=0; i<timeSlotsSortedByDay.get(1).size(); i++){
timeSlotsSortedByDay.get(1).get(i).getHour(); //Do something with hour
timeSlotsSortedByDay.get(1).get(i).getMinute(); //Do something with minute
}