Java For循环在我的代码中失败。根本不执行
我试图执行以下代码,发现for循环根本没有执行。甚至没有得到任何错误。让我澄清一下,“dateList”是一个字符串列表,它不是空的,我已经试过打印它了。“monthList”是字符串的数组列表Java For循环在我的代码中失败。根本不执行,java,for-loop,execution,Java,For Loop,Execution,我试图执行以下代码,发现for循环根本没有执行。甚至没有得到任何错误。让我澄清一下,“dateList”是一个字符串列表,它不是空的,我已经试过打印它了。“monthList”是字符串的数组列表 private ArrayList<String> monthList = new ArrayList<String>(Arrays.asList("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov",
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;
}
}
}
}
private ArrayList monthList=新的ArrayList(Arrays.asList(“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”);
private List dateList=new ArrayList();
private List FinalDatesInmmmdyyyyFormat=new ArrayList();
私人国际年=2019年;
for(int m=0;m日期列表没有添加任何内容。因此,它的大小为0。因为m等于dateList.size(),所以for循环不会执行。您似乎从未向日期列表添加任何内容,因此在启动循环时它是空的。因此,没有任何内容可以循环。寻求调试帮助的问题(“为什么这个代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现该问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅。您希望空列表的大小是多少?哪个for循环没有执行?您如何判断它没有执行?运行它在debugger下一步一步地进行,您可以看到所有的中间值。我已经添加到dateList,它的大小为9。通过打印所有值来检查。在代码中,发布的日期列表直接在for循环之前声明。此代码块中的日期列表中没有任何内容。如果这是在程序中写入的方式,则日期列表的大小将n计算的for循环为0。我已经添加到dateList,它的大小为9。通过打印所有值来检查。它根本不是空的。