for 循环第一次运行时什么都不做?可能是编程逻辑错误?

发布于 2024-11-18 23:29:47 字数 2361 浏览 2 评论 0原文

基本上,在下面的方法中,由于我自己或我的团队未知的原因,最外面的 for 循环将不会在第一次迭代时执行任何代码。我的意思是,在第一轮执行时,它会转到循环末尾并返回到开头,而不执行任何代码。我已经通过eclipse调试证实了这一点。然而,循环在第一个循环之后正常执行。

此方法的作用是从数据库中提取天数,将其放入几周内,然后将这些星期放入一个月中。但是,由于这个奇怪的“故障”,第一个周期不会拉动任何天,随后不会创建要放入该月的一周,但它确实执行循环的计数器,这基本上使数据与循环不同步:

1st run: nothing
2nd run: 1st week
3rd run: 2nd week
...
last run: SECOND LAST WEEK

所以,每次该方法返回月份时,最后一周总是会丢失。

但真正奇怪的是这个循环曾经完美地工作。事实上,我 99.9999% 确信它在我昨晚回家之前正常运行,然后当我今天回来时它突然出现了这个错误。我有那么一点点怀疑的唯一原因是,这种情况绝对没有意义!所以实际上我一定是不小心改变了一些东西并且没有意识到。如果有人能看一下这个并提出可能的原因,我们将非常感激。

谢谢

private Month translateToFormData(List<WeekEntry> weekList, TimeSheetForm timeSheetForm) {
    int monthOfInterest = timeSheetForm.getMonth().getMonthOfYear();
    // month to be returned
    Month month = new Month();
    // Calendars to translate from Date to Calendar
    // loop through weekList
    for (WeekEntry weekEntry : weekList) {
        // week value to be filled
        Week week = new Week();
        Calendar calWeekBeginning = new GregorianCalendar();
        // set the date of the first day in "week" (Sunday)
        calWeekBeginning.setTime(weekEntry.getWeekBeginning());
        week.setWeekBeginning(calWeekBeginning);
        Set<DayEntry> daySet = weekEntry.getDays();
        // Loop through days of the weekList items
        for (DayEntry dayEntry : daySet) {
            // day value to be filled
            Day day = new Day();
            Calendar calDateOfDay = new GregorianCalendar();
            // set the date of the day
            calDateOfDay.setTime(dayEntry.getDateOfDay());
            day.setDate(calDateOfDay);
            // set the hours of the day
            day.setWorkHours(dayEntry.getHours());
            // define the day type
            if (calDateOfDay.get(Calendar.MONTH) != monthOfInterest) {
                day.setType(DayType.BLANK_DAY);
            } else if (calDateOfDay.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calDateOfDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                day.setType(DayType.WEEK_END);
            } else {
                day.setType(DayType.WEEK_DAY);
            }
            // add the day to the week
            week.addDay(day);
        }
        // add the week to the month
        month.addWeek(week);
    }
    return month;
}

Basically, in the method below, for reasons unknown to myself or my team, the outermost for loop will does not execute any code on the first itteration. What I mean by that is, on the first round of execution it goes to the end of the loop and back to the beginning without executing any code. I have confirmed this by debugging through eclipse. However, the loop executes as normal after this first cycle.

What this method is doing is pulling days from a database, putting them into weeks, and putting those weeks into a month. but, because of this weird "glitch" the first cycle does not pull any days and subsequently does not create a week to be put in the month BUT it does execute the counter of the loop which basically leaves the data out off sync with the loop:

1st run: nothing
2nd run: 1st week
3rd run: 2nd week
...
last run: SECOND LAST WEEK

so, everythime the method returns the month, the last week is always missing.

BUT what is really WEIRD is that this loop used to work perfectly. In fact, I am 99.9999% positive that it was functioning before I went home last night and then when I came in today it suddenly developed this error. The only reason that I have that tiny amount of doubt is that IT MAKES ABSOLUTELY NO SENSE FOR THAT TO BE THE CASE! So realistically I must have accidently changed something and not realised it. If anyone could thake a look at this and suggest a possible cause that would be very much appreciated.

Thanks

private Month translateToFormData(List<WeekEntry> weekList, TimeSheetForm timeSheetForm) {
    int monthOfInterest = timeSheetForm.getMonth().getMonthOfYear();
    // month to be returned
    Month month = new Month();
    // Calendars to translate from Date to Calendar
    // loop through weekList
    for (WeekEntry weekEntry : weekList) {
        // week value to be filled
        Week week = new Week();
        Calendar calWeekBeginning = new GregorianCalendar();
        // set the date of the first day in "week" (Sunday)
        calWeekBeginning.setTime(weekEntry.getWeekBeginning());
        week.setWeekBeginning(calWeekBeginning);
        Set<DayEntry> daySet = weekEntry.getDays();
        // Loop through days of the weekList items
        for (DayEntry dayEntry : daySet) {
            // day value to be filled
            Day day = new Day();
            Calendar calDateOfDay = new GregorianCalendar();
            // set the date of the day
            calDateOfDay.setTime(dayEntry.getDateOfDay());
            day.setDate(calDateOfDay);
            // set the hours of the day
            day.setWorkHours(dayEntry.getHours());
            // define the day type
            if (calDateOfDay.get(Calendar.MONTH) != monthOfInterest) {
                day.setType(DayType.BLANK_DAY);
            } else if (calDateOfDay.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calDateOfDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                day.setType(DayType.WEEK_END);
            } else {
                day.setType(DayType.WEEK_DAY);
            }
            // add the day to the week
            week.addDay(day);
        }
        // add the week to the month
        month.addWeek(week);
    }
    return month;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡的优雅 2024-11-25 23:29:47

我想写评论,但我没有积分。我认为您需要提供更多代码。您应该发送 weekList 和 TimeSheetForm 的内容。这些课程有什么变化吗?

(想一想 - 这类问题是为什么您需要自动化测试用例的一个很好的例子;一旦失败您就能看到。)

I'd write a comment but I don't have points. You'd need to provide more code I think. You should send the contents of weekList and TimeSheetForm. Any changes to those classes?

(To think about - this sort of issue is a great example of why you need automated test cases; you'll be able to see as soon as it fails.)

抠脚大汉 2024-11-25 23:29:47

发现问题了,原来是我是个herpaderp!感谢您的建议

应克里斯的要求进行编辑...

事实证明,这与我将对象保存到数据库的方式有关。它在每一行以及日期上放置了时间戳,如果代码在一天中较早的时间运行,然后在最初保存条目时,这会导致它无法检索第一个元素。我不知道为什么只有循环的第一个周期受到影响,但是一旦我将其设置为保存项目而不记录一天中的时间,它就可以正常运行。

Found the problem, it was me being a herpaderp! thanks for your suggestions

Edit at the request of chris...

As it turned out, it was to do with how I was saving the objects to the DB. It was putting a timestamp on each row as well at the date and this was causing it to not retrieve the first element if the code ran at an earlier time of day then when the entries were initially saved. I don't know why it was only the first cycle of the loop that was affected but once I set it to save items without recording the time of day, it runs without any problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文