0

我每 30 分钟创建一次时间段。但我不知道如何在数组中标记周一到周五的每个时间段。这是我想如何标记我的时间段的示例。

MS1 -> Monday 0800-0830
MS2 -> Monday 0830-0900

.
.
TS1 -> Tuesday 0800-0830
TS2 -> Tuesday 0830-0900
...
FS16 -> Friday 0530->0600

//create date(day and time)
public static Date newDate(Day day, int hour, int minute){
    return new Date(day, new TimeSlot(hour, minute));}

//create date with parameters
public static Date newDate(Day day, int minute){
    return new Date(day, new TimeSlot(minute));}

这就是我在时隙类中创建我的时隙的方式。

//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;}}

所以如果我使用二维数组存储。会是这样吗?所以价值指数表示每30分钟aite?

mondayTimeSlot.add(new TimeSlot(0));
mondayTimeSlot.add(new TimeSlot(1));
....
fridayTimeSlot.add(new TimeSlot(15));
4

1 回答 1

0

您可以使用按星期几排序的二维数组来收集数据。像这样的东西:

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

如果你Comparator为你的TimeSlot班级建立了一个定制,你甚至可以对个别的日子进行排序。

//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
}
于 2015-11-24T15:33:04.890 回答