用JAVA语言用 List 实现多级菜单的打印,使用递归打印如下的输出结果

 |手机
 | |游戏手机
 | |5G手机
 | |拍照手机
 |电脑
 | |笔记本
 | | |华硕笔记本
 | | | |华硕i7
 | | | |华硕i5
 | | |联想笔记本
 | | | |联想小新
 | |一体机
 | |台式机
 |电器
 |衣服

思路分析:

        parentId 是设定的商品的级别,当商品Id 与 parentId 的数值相同时,说明该商品是parentId商品的子级,那么定义一级类目的parentId为0,建立一个treeMenu()方法,遍历建立的 List对象,

输出当parentId == 0时的目录,这时已经成功输出了所需要的一级类目,那么可以再一级类目下面寻找对应的二级,三级类目,这时需要用到另外一个循环childMenu(),在childMenu() 中把一级类目的Id以及建立的List对象作为实参传入,在childMenu() 中还是从头遍历建立的List对象,当parentId 等于我们传入的 一级类目的Id时,就是一级类目的子类目,这时把其对应的商品也输出,同时把得到的二级目录的paratId以及List对象作为实参传入,并继续调用childMene() ,这时可以持续调用,直至没有子目录, 循环调用结束,子目录也成功全部输出。

        接下来看代码。

首先先建立一个要建立一个需要的菜单类

public class ProductType {
    private Integer id;
    private String name;
    private Integer parentId;
    private Integer status;

    public ProductType() {
    }

    public ProductType(Integer id, String name, Integer parentId, Integer status) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
        this.status = status;
    }


    public Integer getId() { return id; }

    public void setId(Integer id) { this.id = id; }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public Integer getParentId() { return parentId; }

    public void setParentId(Integer parentId) { this.parentId = parentId; }

    public Integer getStatus() { return status; }

    public void setStatus(Integer status) { this.status = status; }

    @Override
    public String toString() {
        return "ProductType{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", parentId=" + parentId +
                ", status=" + status +
                '}';
    }
}

接下来,可以再主方法里面直接用递归实现菜单的打印

import java.util.ArrayList;
import java.util.List;
public class Test {
    private static List<ProductType> productTypes;

    public static void main(String[] args) {

        productTypes = new ArrayList<>();

        //建立要输入的菜单信息
        productTypes.add(new ProductType(1, "手机", 0, 1));
        productTypes.add(new ProductType(2, "电脑", 0, 1));
        productTypes.add(new ProductType(3, "电器", 0, 1));
        productTypes.add(new ProductType(4, "游戏手机", 1, 1));
        productTypes.add(new ProductType(5, "5G手机", 1, 1));
        productTypes.add(new ProductType(6, "拍照手机", 1, 1));
        productTypes.add(new ProductType(7, "笔记本", 2, 1));
        productTypes.add(new ProductType(8, "一体机", 2, 1));
        productTypes.add(new ProductType(9, "台式机", 2, 1));
        productTypes.add(new ProductType(10, "华硕笔记本", 7, 1));
        productTypes.add(new ProductType(11, "联想笔记本", 7, 1));
        productTypes.add(new ProductType(12, "华硕i7", 10, 1));
        productTypes.add(new ProductType(13, "华硕i5", 10, 1));
        productTypes.add(new ProductType(14, "联想小新", 11, 1));
        productTypes.add(new ProductType(15, "衣服", 0, 1));

        treeMenu(productTypes, " |");


    }

    //建立树形菜单
    private static void treeMenu(List<ProductType> productTypes, String sep) {

        for (ProductType productType : productTypes) {
            if (productType.getParentId() == 0) {//输出一级目录
                System.out.println(sep + productType.getName());
                //循环调用childMenu,直至没有子目录
                childMenu(productTypes, " |" + sep, productType.getId());

            }

        }
    }

    //使用递归,循环调用次方法,直至没有子目录方法退出,并把结果输出
    private static void childMenu(List<ProductType> productTypes, String sep, int id) {
        for (ProductType productType : productTypes) {
            if (productType.getParentId() == id) {
                System.out.println(sep + productType.getName());
                childMenu(productTypes," |" +  sep, productType.getId());
            }
        }
    }


}

版权声明:本文为guapi12原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/guapi12/article/details/123016240