对于循环失败我的代码。根本不执行

时间:2019-02-13 19:20:08

标签: java for-loop execution

我试图执行以下代码,发现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;
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

您似乎从未向dateList添加任何内容,因此在开始循环时它为空。因此,没有任何循环。

答案 1 :(得分:0)

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