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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - 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() + "次");
    }

    // 其他部分保持不变
}