TTT
TTT

Reputation: 186

How to recursively print hierarchical lists with indents

I have a class that consists of a String name and an ArrayList of other instances of the class, known as attachments. Think of it like a lego that can be attached to infinitely many other legos. I need to display this hierarchy in the console with indents (without passing in any parameters) and I am not sure the best way to do so:

Expected Output:

+ PowerSource
    + Appliance
         + Extension
         + Module
    + Lamp
    + Appliance
         + Module

Current Output:

+ PowerSource
    + Appliance
    + Extension
    + Module
    + Lamp
    + Appliance
    + Module

I have the display() method to the point where I am able to indent once, but then I cannot seem to get attachments of attachments to indent twice. Any help would be appreciated

package components;

import java.util.ArrayList;
import java.util.List;

public class MyTest {

    private String name;
    private List<MyTest> attachments;

    public MyTest(String name) {
        this.name = name;
        attachments = new ArrayList<MyTest>();
    }

    public void attach(MyTest newLoad) {
        attachments.add(newLoad);
    }

    public void display() {
        System.out.print("+ " + toString() + "\n");
        if (attachments.size() > 0) {
            for (MyTest load : attachments) {
                System.out.print("    ");
                load.display();
            }
        }
    }

    @Override
    public String toString() {
      return name;
    }

    public static void main(String[] args) {
        MyTest a = new MyTest("PowerSource");
        a.attach(new MyTest("Appliance"));
        a.attach(new MyTest("Appliance"));
        MyTest l = new MyTest("Lamp");
        l.attach(new MyTest("Extension"));
        a.attach(l);

        a.display();
    }
}

Upvotes: 1

Views: 815

Answers (2)

Tarum
Tarum

Reputation: 993

its normal. when recursive call made, program doesnt know how much indent it has to made. its always making one indent. program need to know depth of your list hierarchy.

i mean, when Appliance called its display, it have to indent two times. because their elements need to be double intended. but it dont have that information.

so you can create filed int depth=0; for base object. and edit attach like this.

public void attach(MyTest newLoad) {
    newLoad.setDepth(this.depth + 1);
    attachments.add(newLoad);
}

then edit display method like this and add indent method.

private void indent(int depth){
   if(depth > 0){
        System.out.print("    ");
        indent(depth-1);
    }
}
public void display() {
    System.out.print("+ " + toString() + "\n");
    for (MyTest load : attachments) {
         this.indent(this.depth);
         load.display();
    }
 }

I haven`t test this but you will get the main idea from those examples

Upvotes: 2

VGR
VGR

Reputation: 44335

Pass the prefix as a parameter:

public void display() {
    display("");
}

private void display(String prefix) {
    System.out.println(prefix + "+ " + name);
    for (MyTest load : attachments) {
        load.display(prefix + "    ");
    }
}

Note that there is no need for if (attachments.size() > 0), since the loop will simply do nothing if attachments is empty.

Upvotes: 2

Related Questions