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();
}
}
}
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;
}
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();
}
}
}
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;
}
解决方案 »
- 请教个关于NEW的问题
- Core java Volume II原文中的一处错误
- 求教:如果将一个string日期(2010-02-01)的格式表示成01-Jan-2010
- 如何获取压缩文件
- 菜鸟请教 非一般的游标溢出 问题!
- 怎样取得JInternalFrame的父窗口
- 请教一个关于JScrollPane的问题
- 怎么手动给ie加java plugin?不通过安装jdk?直接用jbuilder下的jdk!
- Java Applet 中变量的问题请教.
- 请问哪里有sqlserver2000的jdbc驱动程序?
- 这段网上代码的synchronized加的莫名其妙,还是理解不了.....
- 如何使新建的ArrayList的元素取值范围为Integer 1到24?
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
如果用for each循环里面可以进行增删不 +10086