对于我的代码中的循环失败。根本不执行[重复]

问题描述 投票:-2回答:2

这个问题在这里已有答案:

我试图执行以下代码,发现for循环根本没有执行。即使没有收到任何错误。让我明确一下,“dateList”是一个字符串列表,它不是空的,我已经尝试过打印了。 “monthList”是字符串的ArrayList。

private ArrayList<String> monthList = new ArrayList<String>(Arrays.asList("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"));
private List<String> dateList = new ArrayList<>();
private List<Date> finalDatesInMMMDDYYYYFormate = new ArrayList<>();
private int year = 2019;



 for (int m=0; m<dateList.size(); m++){
        System.out.println("Date at position ------ "+m+" is -------"+dateList.get(m));
    } // length of this list is 9 & it prints eberything from 0 to 9. It is not empty.
    int assignYear = year;
    for (int k=1; k<dateList.size(); k++){
        if (dateList.get(k).substring(0,2)==dateList.get(k-1).substring(0,2) || dateList.get(k).substring(0,2) == monthList.get(0)){
            finalDatesInMMMDDYYYYFormate.add(Utils.parseDate(dateList.get(k)+Integer.toString(assignYear).replaceAll("[^a-zA-Z0-9]",""), new SimpleDateFormat("MMMddyyyy")));
        }
        else if (dateList.get(k).substring(0,2) == monthList.get(11)){
            finalDatesInMMMDDYYYYFormate.add(Utils.parseDate(dateList.get(k)+Integer.toString(assignYear-1).replaceAll("[^a-zA-Z0-9]",""), new SimpleDateFormat("MMMddyyyy")));
            assignYear = assignYear-1;
        }
        else {
            for (int l=0; l<monthList.indexOf(dateList.get(k-1).substring(0,2)); l++){
                if (dateList.get(k).substring(0,2) == monthList.get(l)){
                    finalDatesInMMMDDYYYYFormate.add(Utils.parseDate(dateList.get(k)+Integer.toString(assignYear).replaceAll("[^a-zA-Z0-9]",""), new SimpleDateFormat("MMMddyyyy")));
                    break;
                }
                else {
                    finalDatesInMMMDDYYYYFormate.add(Utils.parseDate(dateList.get(k)+Integer.toString(assignYear-1).replaceAll("[^a-zA-Z0-9]",""), new SimpleDateFormat("MMMddyyyy")));
                    assignYear--;
                    break;
                }
            }
        }
    }
java for-loop execution
2个回答
1
投票

你似乎永远不会向dateList添加任何东西,所以当你开始循环时它是空的。因此没有什么可以循环的。


0
投票

dateList没有添加任何内容。因此它的大小为0.因为m等于dateList.size(),for循环不会执行。

© www.soinside.com 2019 - 2024. All rights reserved.