如何在Java中以树结构显示列表数据?

编程入门 行业动态 更新时间:2024-10-25 04:18:24
本文介绍了如何在Java中以树结构显示列表数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个 arrayList,我想以分层结构显示.

I have an arrayList and I want to display in a hierarchal structure.

我希望结果看起来像这样.如果它没有任何子节点,我希望该项目用连字符缩进:

I want the results to look like this. If it doesn't have any child nodes I want the item indented with a hyphen:

User Design
    Lectures
        Week 1
            -Apr 5
            -Apr 8
        Week 2
            -Apr 12
    Activities
        Personas
            Male
                -George
            Female
                -Allison
                -Jessica

我正在尝试使用递归方法显示列表,但没有获得所需的结果.这是我迄今为止尝试过的:

I am trying to display the list using a recursive method but not getting the desired results. This is what I've tried so far:

import java.util.*;

class Materials {
    public int id;
    public String content;
    public int pid; 

    public Materials(int id, String content, int pid) {
        this.id = id;
        this.content = content;
        this.pid = pid; 
    }
}
public class nestedList {

public static ArrayList<Materials> material;

public static void main(String[] args) {
    material = new ArrayList<Materials>();
    material.add(new Materials(1,"User Design", 0));
    material.add(new Materials(2,"Lectures", 1));
    material.add(new Materials(3,"Activities", 1));
    material.add(new Materials(4,"Week 1", 2));
    material.add(new Materials(5,"Apr 5", 4));
    material.add(new Materials(6,"Apr 8", 4));
    material.add(new Materials(7,"Week 2", 2));
    material.add(new Materials(8,"Apr 12", 7));
    material.add(new Materials(9,"Personas", 3));
    material.add(new Materials(10,"Male", 9));
    material.add(new Materials(11,"Female", 9));
    material.add(new Materials(12,"George", 10));
    material.add(new Materials(13,"Allison", 11));
    material.add(new Materials(14,"Jessica", 11));

    displayContent(material);

}

static void displayContent(ArrayList<Materials> materials) {
    ArrayList<Materials> childs = new ArrayList<Materials>();
    for (Materials material : materials) {
        childs = selectChild(material.id);
        System.out.println(material.content);
        displayContent(childs);
    }

}

static ArrayList<Materials> selectChild(int id) {
    ArrayList<Materials> list = new ArrayList<Materials>();

    for (int i = 0; i < material.size(); i++) {
        if(material.get(i).pid == id) {
            list.add(material.get(i));
        }
    }

    return list;
}
}

当我运行此代码时,arrayList 中的项目重复了太多次.它最初显示正确的层次结构,然后开始重复随机变化.

When I run this code the items in the arrayList repeats too many times. It initially displays the correct hierarchal structure and then starts to repeat with random variations.

有人能指出我正确的方向吗?

Can anyone please point me in the right direction?

推荐答案

问题在于,在您第一次调用 displayContent 时,您传递的是整个材料列表.所以它会这样做:

The problem is that in your first call to displayContent, you're passing the entire list of materials. So it's going to do this:

Display the entire hierarchy for "User Design"
Display the entire hierarchy for "Lectures"
Display the entire hierarchy for "Activities"
etc.

您真正想做的是在初始调用中仅将根元素传递给 displayContent.所以在你的主要:

What you really want to do is pass only the root element to displayContent in your initial call. So in your main:

ArrayList<Materials> topLevel = selectChild(0);
displayContent(topLevel);

这篇关于如何在Java中以树结构显示列表数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-16 07:23:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/881942.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中以   结构   数据   如何在   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!