惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
博客园 - 【当耐特】
小众软件
小众软件
博客园 - Franky
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
雷峰网
雷峰网
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
S
Security @ Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
Y
Y Combinator Blog
O
OpenAI News
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
Kaspersky official blog
Cloudbric
Cloudbric

博客园 - timseng

ai辅助升级前端安全更新(贼快!hh openclaw的token又耗完了,先不冲了,记录下折腾踩坑过程 (如果需要协助安装500,帮忙卸载250[doge]) VS Code我的配置(持续更新) 推广:flowable工作流引擎就是高级版本的数据库而已(一) springboot升级3x导致邮件发送失败:错误1 jakarta.mail.NoSuchProviderException: smtp nice du更好的du分析文件占用工具ncdu 使用MITMProxy转发https请求到本地、保存鉴权给本地请求(二) 前端发布shell脚本 virtualBox环境Ubuntu升级后太卡,转debian很丝滑 收藏:加不加「/」?Nginx location 路径与 proxy_pass 的规律 使用visjs分析flowable流程数据 浏览器标签页多行显示:使用Floorp浏览器 最先进的跨平台 Firefox 衍生品 开源之光 开源WAF:ModSecurity探究与部署 动态条件实现java mysql备份恢复云端之核心:更新扫描MySQL库里的所有表的UPDATE_TIME,若发生变动就mysqldump 使用MITMProxy转发请求到本地、保存鉴权给本地请求 这款Ubuntu的默认壁纸水母是真漂亮啊 笑死!'m not going to tell you who, but I know someone who recently locked himself out of the system he was using for an article covering iptables by forgetting the SSH rule. itext的PdfPCell中设置行间距失败的问题
Java递归函数计算递归次数出错
timseng · 2024-01-11 · via 博客园 - timseng

背景:构造组织架构树时,使用了递归填充子节点,为防止环状的错误数据导致递归无法结束,记录递归的次数,超过一定数量后终止递归

问题:用户记录递归次数的变量在节点跳转的时候被重新赋值,无法正确记录

public Depart genDepartTreeFromRoot() throws Exception {
    Depart root = Depart.builder().id("").name("").parentDepart(null).build();

    //防止部门数据有错误的环状结构导致死循环 最多填充1000000次
    fillDepartChildCount=0;
    fillDepartChildAndUserList(root,fillDepartChildCount);
    log.info("genDepartTreeFromRoot success  当前已填充子节点 {} 次 depart {}",fillDepartChildCount,root);

    return root;
  }
  public void fillDepartChildAndUserList(Depart depart,Integer fillDepartChildCount) throws Exception {
    if(fillDepartChildCount++>100000000){
      log.error("部门数据可能有错误的环状结构导致死循环  当前已填充子节点 {} 次 depart {}",fillDepartChildCount,depart);
      throw new Exception("部门数据可能有错误的环状结构导致死循环 当前已填充子节点"+fillDepartChildCount+"次");
    }

    List<User> userList = getDepartUserList(List.of(depart.getId()));
    depart.setUserList(userList);

    List<Depart> childList=getChildListLevel1(depart.getId());

    if(childList!=null && childList.size()>0){
      depart.setHasChild(true);
      depart.setChildren(childList);

      for (Depart departChild : childList) {
        departChild.setParentDepart(depart);
        fillDepartChildAndUserList(departChild,fillDepartChildCount);
      }
    }else{
      depart.setHasChild(false);
    }

  }

 解决:ChatGPT:

在 Java 中,基本类型(例如 int)是按值传递的。在你的代码中,fillDepartChildCount 是一个 Integer 对象,并不是基本类型。当你传递 fillDepartChildCountfillDepartChildAndUserList 方法时,传递的是对象引用的副本,而不是对象本身。这就导致了在递归调用过程中,虽然值被增加了,但是在上一级调用中的值并没有被更新。

为了解决这个问题,你可以使用一个封装了 fillDepartChildCount 的可变对象,比如一个数组或者一个包含计数值的对象。这样,在递归调用中,你修改的是数组或者对象内部的值,而不是传递的引用:

使用 AtomicInteger 类或者封装计数器的对象可以确保递归调用中正确地更新计数器的值,而不会出现因为传递引用导致值未更新的情况。

public Depart genDepartTreeFromRoot() throws Exception {
    Depart root = Depart.builder().id("").name("").parentDepart(null).build();

    // 使用可变对象封装计数器
    AtomicInteger fillDepartChildCount = new AtomicInteger(0);
    fillDepartChildAndUserList(root, fillDepartChildCount);
    log.info("genDepartTreeFromRoot success 当前已填充子节点 {} 次 depart {}", fillDepartChildCount.get(), root);

    return root;
}

public void fillDepartChildAndUserList(Depart depart, AtomicInteger fillDepartChildCount) throws Exception {
    // 在方法中使用 getAndIncrement() 方法来递增计数器
    if (fillDepartChildCount.getAndIncrement() > 100000000) {
        log.error("部门数据可能有错误的环状结构导致死循环 当前已填充子节点 {} 次 depart {}", fillDepartChildCount.get(), depart);
        throw new Exception("部门数据可能有错误的环状结构导致死循环 当前已填充子节点" + fillDepartChildCount.get() + "次");
    }

    // 其他部分保持不变
}