递归使用案例

本文深入探讨了递归的概念,通过多个实际案例展示了递归在解决复杂问题时的威力。从计算阶乘到遍历文件系统,递归提供了简洁且优雅的解决方案。同时,文章也讨论了递归的潜在风险,如堆栈溢出,并提供了一些优化策略。
//交接班记录补算
	public void handMakeup()
	{
		//获取当前时间
		Date date = new Date();
		List<ChildrenSystem> list = childrenSystemDao.getByName(null);
		for(int i=0; i<list.size(); i++)
		{
			System.out.println(list.get(i).getId()+"----------------");
			diGui(list.get(i).getId(), date);
		}
	}
	
	public void diGui(Long systemId, Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		List<HandRecord> handRecords = getByEndTimeIsNull(systemId);

		HandRecord handRecord = handRecords.get(0);
		Date wtBegin = handRecord.getWtBegin();
		String time = sdf.format(wtBegin);
		
		Long systemId2 = handRecord.getSystemId();
		if(systemId2 == 1L)
		{
			systemId2 = 0L;
		}
		
		//通过开始时间去倒班时间表中查询记录
		String dutyName = dutyTimeDao.getDutyNameByStartTime(time, systemId2);
		DutyTime dutyTime = dutyTimeDao.getDutyTimeByDutyName(dutyName, systemId2);
		
		try {
			if(sdf.parse(dutyTime.getStartTime()).getTime() > sdf.parse(dutyTime.getEndTime()).getTime())
			{
				wtBegin = new Date(wtBegin.getTime() + sdf.parse(dutyTime.getEndTime()).getTime() - sdf.parse(time).getTime()+24*60*60*1000);
			}else{
				wtBegin = new Date(wtBegin.getTime() + sdf.parse(dutyTime.getEndTime()).getTime() - sdf.parse(time).getTime());
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//若成立,则代表有间隔的交接班记录没有产生,应终结该条记录并产生新的记录
		if(wtBegin.getTime() < date.getTime())
		{
			for(int i=0; i<handRecords.size(); i++)
			{
				HandRecord handRecord2 = handRecords.get(i);
				//现将上一条记录终结
				handRecord2.setWtEnd(wtBegin);
				save(handRecord2);
				//再产生一条新的记录
				//首先确定当班班值的名字
				String dutyName2 = dutyTimeDao.getDutyNameByStartTime(sdf.format(wtBegin), systemId2);
				
				DutyTable dutyTable = dutyTableDao.getDutyTableByDate(wtBegin, systemId2);
				ArrayList<String> list = new ArrayList<String>();
		
				list.add(dutyTable.getDuty1());
				list.add(dutyTable.getDuty2());
				list.add(dutyTable.getDuty3());
				list.add(dutyTable.getDuty4());
				list.add(dutyTable.getDuty5());
				list.add(dutyTable.getDuty6());
				list.add(dutyTable.getDuty7());
				list.add(dutyTable.getDuty8());
				list.add(dutyTable.getDuty9());
				
				//接班的班次号
				int takeTeamId = 0;
				
				for(int j=0; j<list.size();j++)
				{
					if(list.get(j).equals(dutyName2))
					{
						takeTeamId = j+1;
						break;
					}
				}
				handRecord2.setId(null);
				handRecord2.setWtBegin(wtBegin);
				handRecord2.setWtEnd(null);
				handRecord2.setTeamId(takeTeamId);
				save(handRecord2);
				
			}
			System.out.println("装备进行递归");
			//进行递归
			diGui( systemId, date);
		}
		
		return;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值