0

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;
}
}

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?

6
  • I do not think List is suitable for hierarchal structure Commented Apr 30, 2018 at 7:37
  • I am given a list of N materials. What data structure would you recommend that I use? Commented Apr 30, 2018 at 7:42
  • Duplicate of Nested indented output with a recursion method Commented Apr 30, 2018 at 7:50
  • @Dukeling, thanks for that. The tab count mentioned in my answer can be managed by that answer. Commented Apr 30, 2018 at 7:53
  • So what's your question? What is the specific problem you're having? As written, your question is too broad. Commented Apr 30, 2018 at 13:49

2 Answers 2

1

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.

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Oh that makes sense! I decided to try a different test case where "User Design" is no longer the root element, but both "Lectures" & "Activities" have a parentID of '0'. How can I display both at the same time without making 2 separate calls to displayContent?
The code I show will do that. It gets all elements that have a parentID of 0, using your selectChild function. Just pass that list to the displayContent method.
1
static void displayContent(ArrayList<Materials> materials) {
    ArrayList<Materials> childs = new ArrayList<Materials>();
    for (Materials material : materials) {
        childs = selectChild(material.id);
        if(childs.isEmpty()) {
            System.out.print(" - ");
        }
        System.out.println(material.content);
        displayContent(childs);
    }

}

Checking if the list is empty before you print the material content will add the - for the last element.

For the systematic tabs, you will have to count the traversals (child of child of child is 3, for example) and add that many tabs. This answer can help with that. Thanks Dukeling for pointing it out.

1 Comment

Thank you for that! But I'm still having an issue with the fact that the items in the arrayList repeats too many times. It initially displays the correct hierarchal structure and then starts to repeat with random variations. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.