java tree添加编号

原文地址 : 

Java 树形结构生成序号/编号_不像程序猿的程序员的博客-CSDN博客_java 树形结构编号icon-default.png?t=M276https://blog.csdn.net/qq_44538738/article/details/120811469?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-120811469.pc_agg_new_rank&utm_term=java+%E6%A0%91%E5%BD%A2%E7%BB%93%E6%9E%84%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%BA%8F%E5%8F%B7&spm=1000.2123.3001.4430

一、描述

public class TreeVO {

private String sqNum;

private Long id;

private Long parentId;

    private List<TreeVO> childList;
}

用上面树形结构生成下面这样的序号

1-1
1-1.1
1-1.2
1-2
1-2.1
1-2.1

二、代码实现

public class TreeSequenceNumber {

    public static void main(String[] args) {
        TreeSequenceNumber treeSequenceNumber = new TreeSequenceNumber();
        treeSequenceNumber.createSeqNum();
    }


    void createSeqNum() {
        //组装数据
        List<TreeVO> treeList = createData();

        //根节点生成序号
        for (int i = 0; i < treeList.size(); i++) {
            TreeVO treeVO = treeList.get(i);
            String parentSeqNum = i + 1 + "";
            treeVO.setSqNum(parentSeqNum);
            recursiveLoopCreateSeqNum(treeVO.getChildList(), parentSeqNum);
        }

        //打印结果
        System.out.println(JSON.toJSONString(treeList));
    }


    void recursiveLoopCreateSeqNum(List<TreeVO> treeList, String parentSeqNum) {
        if (null == treeList || treeList.size() < 1) {
            return;
        }
        for (int i = 0; i < treeList.size(); i++) {
            TreeVO treeVO = treeList.get(i);
            String childSeqNum = parentSeqNum + "-" + (i + 1);
            treeVO.setSqNum(childSeqNum);
            recursiveLoopCreateSeqNum(treeVO.getChildList(), childSeqNum);
        }
    }


    /**
     * 创建数据
     */
    List<TreeVO> createData() {
        //树形结构数据列表
        List<TreeVO> treeVOList = new ArrayList<>();

        //造数据
        TreeVO treeVO1 = new TreeVO();
        treeVO1.setParentId(5L);
        treeVO1.setId(1L);

        TreeVO treeVO2 = new TreeVO();
        treeVO2.setParentId(5L);
        treeVO2.setId(2L);

        TreeVO treeVO3 = new TreeVO();
        treeVO3.setParentId(6L);
        treeVO3.setId(3L);

        TreeVO treeVO4 = new TreeVO();
        treeVO3.setParentId(6L);
        treeVO4.setId(4L);


        List<TreeVO> treeVO5ChildList = new ArrayList<>();
        treeVO5ChildList.add(treeVO1);
        treeVO5ChildList.add(treeVO2);

        TreeVO treeVO5 = new TreeVO();
        treeVO5.setId(5L);
        treeVO5.setParentId(0L);
        treeVO5.setChildList(treeVO5ChildList);

        List<TreeVO> treeVO6ChildList = new ArrayList<>();
        treeVO6ChildList.add(treeVO3);
        treeVO6ChildList.add(treeVO4);

        TreeVO treeVO6 = new TreeVO();
        treeVO6.setId(6L);
        treeVO6.setParentId(0L);
        treeVO6.setChildList(treeVO6ChildList);

        treeVOList.add(treeVO5);
        treeVOList.add(treeVO6);

        return treeVOList;
    }

}

最终结果:

[{
	"childList": [{
		"id": 1,
		"parentId": 5,
		"sqNum": "1-1"
	}, {
		"id": 2,
		"parentId": 5,
		"sqNum": "1-2"
	}],
	"id": 5,
	"parentId": 0,
	"sqNum": "1"
}, {
	"childList": [{
		"id": 3,
		"parentId": 6,
		"sqNum": "2-1"
	}, {
		"id": 4,
		"sqNum": "2-2"
	}],
	"id": 6,
	"parentId": 0,
	"sqNum": "2"
}]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值