递归是不能通过return的方式跳出的,可以通过抛异常的方式一步跳出递归。

例如以下递归找第一个摄像头,递归找到后通过抛异常抛出目标结果,然后在调用的地方catch这个结果:

/**
* 递归找到第一个摄像头
* <pre>
* 1.当找到摄像头即通过抛出Exception的方式跳出递归,返回key
* </pre>
* @param areaVideoTreeBOList
* @return
*/
private void getFirstAreaVideoBO(List<AreaVideoTreeBO> areaVideoTreeBOList){
if(!CollectionUtils.isEmpty(areaVideoTreeBOList)){
for(AreaVideoTreeBO areaVideoTreeBO:areaVideoTreeBOList){
if(areaVideoTreeBO.getLevel() != null && areaVideoTreeBO.getLevel() == 7){
throw CommonException.exception(areaVideoTreeBO.getKey());
}else {
getFirstAreaVideoBO(areaVideoTreeBO.getChildren());
}
}
}
}

catch上面递归方法抛出的结果
String key = "";
try{
getFirstAreaVideoBO(areaVideoTreeBOList);
}catch (Exception e){
//跳出递归抛出的key
key = e.getMessage();
}

相关文章:

  • 2021-12-19
  • 2021-12-29
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2021-12-02
  • 2021-12-16
相关资源
相似解决方案