java高手过来瞧瞧什么情况?
java.util.ConcurrentModificationException调用这行findCurrentWeeK(List<List<Date>> myMonthweeks,Date weekD)报错,百度上看在集合循环时候不能改变(如add.remove)?我不明白下面为什么?下面是代码
public String listKaoHe() {
Connection conn = null;
try{
conn = WAFDBConnectionFactory.getInstance().getConnection();
years=DataItemProxy.getDataItemListByDataId("years", conn);
months=DataItemProxy.getDataItemListByDataId("months", conn);
if(curentYear==null || "".equals(curentYear))
curentYear=getCurrentYear();
if(currentMonth==null || "".equals(currentMonth))
currentMonth=findCurrentMonth();
if(curentYear!=null && !"".equals(curentYear) && currentMonth!=null &&!"".equals(currentMonth)){
monthweeks=getWeekOfYear(curentYear,currentMonth).get(currentMonth);
}else{
monthweeks=getWeekOfYear("","").get(currentMonth);
}
Date start = null,end=null;
Calendar ca=Calendar.getInstance();
if(Integer.parseInt(currentMonth)==(ca.get(Calendar.MONTH)+1)){
String curr=curentYear;
if(currentMonth.length()==1)
curr="0"+currentMonth;
Date monthStart=strToDate(curentYear+"-"+curr+"-01");
int nextcurr=Integer.parseInt(currentMonth)+1;
String next=null;
if(currentMonth.length()==1 && Integer.parseInt(currentMonth)>8){
next=new Integer(nextcurr).toString();
}else{
next="0"+new Integer(nextcurr).toString();
}
Date monthend=strToDate(curentYear+"-"+next+"-01");
List<List<Date>> a=monthweeks;
if(monthStart.before(new Date()) && monthend.after(new Date()))
currentWeek=findCurrentWeeK(a,new Date());
}
if(currentWeek==0){
HttpSession session=this.getRequest().getSession();
WafUser user=(WafUser)session.getAttribute("user");
String whereSql=" where username=? and startday<=? and endday>=? ";
Object[] paras=new Object[]{user.getUserId(),getTamp(start),getTamp(end)};
List<Attendance> atts= AttendanceProxy.findAttendance(AttendanceProxy.searchSql + whereSql, paras,0,Integer.MAX_VALUE,conn);
if(atts!=null)
for (Attendance attendance : atts){
if(attendance.getAttendanceDay()>0 || attendance.getDutyDay()>0 || attendance.getInfo1()!=null){
attCount=1;
break;
}
}
}
this.saveLog("进入页面成功");
return SUCCESS;
}catch(SQLException e){
this.saveLog("进入页面失败");
e.printStackTrace();
return ERROR;
}finally{
try{
WAFDBConnectionFactory.getInstance().closeConnection(conn);
}catch(SQLException e){
e.printStackTrace();
}
}
}
--------------------编程问答-------------------- 在这夜深人静的时候,一过来就瞧见这么一大堆的代码,刚补好的心情一转弯就被打破了。是何居心啊 --------------------编程问答-------------------- 哥啊,我等着救命呢 --------------------编程问答-------------------- 就是List、Map什么的,不要在for、while中执行add或remove操作(可能会影响循环的条件判断)。
public int findCurrentWeeK(List<List<Date>> myMonthweeks,Date weekD){
int k=1;
int f=0;
if(myMonthweeks!=null)
for (int i=0,j=myMonthweeks.size();i<j;i++) {
Date da1=myMonthweeks.get(i).get(0);
Date da2=myMonthweeks.get(i).get(1);
if(da1.equals(weekD) || da2.equals(weekD) || (da1.before(weekD) && da2.after(weekD))){
f=k;
}
k++;
}
return k;
}
private static Map<String,List<List<Date>>> getWeekOfYear(String selectYear,String selectMonth) {
Map<String,List<List<Date>>> maps=new HashMap<String,List<List<Date>>>();
List<List<Date>> list=new ArrayList<List<Date>>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
DecimalFormat df = new DecimalFormat("00");
Calendar calendar = Calendar.getInstance();
if(selectYear!=null && !"".equals(selectYear) && selectMonth!=null && !"".equals(selectMonth)){
try{
if(selectMonth.length()==1)
selectMonth="0"+selectMonth;
Date selectDate=sdf.parse(selectYear+selectMonth+"01");
calendar.setTime(selectDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
int year = calendar.get(Calendar.YEAR);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.WEEK_OF_YEAR, 1);
int week = 1;
while (calendar.get(Calendar.YEAR) <= year) {
List<Date> weekdate=null;
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
weekdate=new ArrayList<Date>();
if(week==1 && !isCurrentYear(calendar.getTime())) {
calendar.add(Calendar.DATE, 1);
continue;
}
df.format(week++);
weekdate.add(calendar.getTime());
calendar.add(Calendar.DATE, 6);
weekdate.add(calendar.getTime());
if(week>=50 && !isCurrentYear(calendar.getTime())){
break;
}
list.add(weekdate);
if((week-1)%4==0){
maps.put(new Integer((week-1)/4).toString(), list.subList((week-1)-4, (week-1)));
}
}
calendar.add(Calendar.DATE, 1);
}
return maps;
}
LZ可以这么用:
--------------------编程问答-------------------- 看异常的类型,应该是并发访问集合对象时抛出的异常。
import java.util.ArrayList;
import java.util.List;
public class ListRemoveDemo
{
public static void main(String[] args)
{
//初始化一些数据
List<Integer> list = new ArrayList<Integer>();
for(int i = 0 ; i < 100; i++)
{
list.add(i);//这里为啥可以add?因为for循环里的list结构变化,对i的取值没有影响
}
//将偶数从list中去掉
List<Integer> removeList = new ArrayList<Integer>();
Integer temp;
for(int i = 0; i<list.size();i++)
{
temp = list.get(i);
if (temp % 2 == 0)
{
removeList.add(temp);//这里不能直接remove,因为list的结构变化,会导致size()变化,影响循环体i的判断
}
}
//一并删除
list.removeAll(removeList);
}
}
有两点需要注意的内容:
1.集合类型,必须要采用线程安全的类型,否则,数据可能不同步。
2.遍历集合对象时,尽量使用迭代器,使用迭代器进行数据的增删改查操作。
利用集合的size方法进行for循环遍历,这种编程方案,在单线程处理数据时可以采用,
但也要注意:循环体里面不能进行集合对象的增删操作,否则size的返回值会产生变化,造成不可预期的错误。 --------------------编程问答-------------------- 骗下分,没分了,我也没学到数据库,不好意思帮不了你了 --------------------编程问答-------------------- 我对这个不是很懂,但至少我知道你有错误 --------------------编程问答--------------------
太长了!!~~!~!~!~!~!~
http://www.javadad.com --------------------编程问答--------------------
靠谱+10086 --------------------编程问答--------------------
这个非常全面了 --------------------编程问答-------------------- 在循环中对list add remove会影响原listsize --------------------编程问答-------------------- 想问下
--------------------编程问答-------------------- 使用iterator在循环中删掉某一项。 --------------------编程问答--------------------
就是List、Map什么的,不要在for、while中执行add或remove操作(可能会影响循环的条件判断)。
LZ可以这么用:
import java.util.ArrayList;
import java.util.List;
public class ListRemoveDemo
{
public static void main(String[] args)
{
//初始化一些数据
List<Integer> list = new ArrayList<Integer>();
for(int i = 0 ; i < 100; i++)
{
list.add(i);//这里为啥可以add?因为for循环里的list结构变化,对i的取值没有影响
}
//将偶数从list中去掉
List<Integer> removeList = new ArrayList<Integer>();
Integer temp;
for(int i = 0; i<list.size();i++)
{
temp = list.get(i);
if (temp % 2 == 0)
{
removeList.add(temp);//这里不能直接remove,因为list的结构变化,会导致size()变化,影响循环体i的判断
}
}
//一并删除
list.removeAll(removeList);
}
}
size()值会变化,可以考虑用一个中间变量把size先存起来,和上边的意思差不多 --------------------编程问答-------------------- 有点长···· --------------------编程问答-------------------- 没看楼主的代码,但是这个错误我也遇到过,应该是在遍历集合的时候又进行了remove操作,建议先放在其他地方,在遍历完之后一并移除 --------------------编程问答-------------------- http://www.blogjava.net/EvanLiu/archive/2008/08/31/224453.html --------------------编程问答--------------------
就是List、Map什么的,不要在for、while中执行add或remove操作(可能会影响循环的条件判断)。
LZ可以这么用:
import java.util.ArrayList;
import java.util.List;
public class ListRemoveDemo
{
public static void main(String[] args)
{
//初始化一些数据
List<Integer> list = new ArrayList<Integer>();
for(int i = 0 ; i < 100; i++)
{
list.add(i);//这里为啥可以add?因为for循环里的list结构变化,对i的取值没有影响
}
//将偶数从list中去掉
List<Integer> removeList = new ArrayList<Integer>();
Integer temp;
for(int i = 0; i<list.size();i++)
{
temp = list.get(i);
if (temp % 2 == 0)
{
removeList.add(temp);//这里不能直接remove,因为list的结构变化,会导致size()变化,影响循环体i的判断
}
}
//一并删除
list.removeAll(removeList);
}
}
补充:Java , Java SE