Reputation: 331
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?
Upvotes: 0
Views: 978
Reputation: 134125
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);
Upvotes: 1
Reputation: 2230
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.
Upvotes: 1