在 toString() 方法中不发生递归

我正在创建一个带有Bags 和Items的复合模式,其中Bags 可以包含Items 但Items 不能包含任何内容。


当我toString()在根上使用-method 时,我希望其中的每一个Entity都Bag以“自然语言”打印。目前看起来递归在toString()-method 中被破坏了,我不知道为什么。我尝试System.out.println(1);在while-loop之前使用,它只打印一次,这就是为什么我认为递归被破坏了。


Item-班级;


public class Item extends Entity {


public Item(String theName) {

    this.name = theName;

}


public String toString() {

    String output = String.format("a %s", name);

    return output;

}

}

Bag-班级


import java.util.*;


public class Bag extends Entity {

private List<Entity> children = new ArrayList<Entity>();


public Bag(String theName) {

    this.name = theName;

}


public void add(Entity entity) {

    children.add(entity);

}


public String toString() {

    String output = String.format("a %s", name);

    Iterator<Entity> itemIterator = children.iterator();

    output += " containing ";


    Entity current = itemIterator.next();

    while (itemIterator.hasNext()) {

        output += current.toString();


        Entity next = itemIterator.next();

        if (next instanceof Item) {

            output += ", ";

        } else if (next instanceof Bag) {

            output += " and ";

        }

        current = next;

    }

    return output;

}

}

Entity-班级;


public abstract class Entity {


protected String name;

protected int weight;


public abstract String toString();

public abstract int getWeight();


}

MyFrame包含main- 方法;


哆啦的时光机
浏览 144回答 3
3回答

翻阅古今

改变return&nbsp;output;到return&nbsp;output&nbsp;+&nbsp;current;您没有将最后一个元素附加到您的输出中。此外,您Iterator::next在检查之前调用Iterator::hasNext,如果您有一个空的Bag,则会有一个NoSuchElementException.

万千封印

在其他答案中已经批评了迭代器的使用;还应注意边界条件:0 或 1 个儿童。public String toString() {&nbsp; &nbsp; String output = String.format("a %s", name);&nbsp; &nbsp; if (!children.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; // Unfortunately because of the recursion the following&nbsp; &nbsp; &nbsp; &nbsp; // cannot be done.&nbsp; &nbsp; &nbsp; &nbsp; //String list = children.stream()&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; .map(Entity::toString).collect(Collectors.join(", "));&nbsp; &nbsp; &nbsp; &nbsp; //output += list.replaceFirst(", ([^,]*)$", " and $1");&nbsp; &nbsp; &nbsp; &nbsp; // Iterating from the back reversed, allows an easy&nbsp; &nbsp; &nbsp; &nbsp; // discrimination of comma and "and."&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder list = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; String separator = " and ";&nbsp; &nbsp; &nbsp; &nbsp; ListIterator<Entity> itemIterator = children.listIterator(children.size());&nbsp; &nbsp; &nbsp; &nbsp; while (itemIterator.hasPrevious()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Entity entity = itemIterator.previous();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.insert(0, entity.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (itemIterator.hasPrevious()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.insert(0, separator);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; separator = ", ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; output += list.toString();&nbsp; &nbsp; }&nbsp; &nbsp; return output;}

一只名叫tom的猫

你在 while 条件下有错误if (!itemIterator.hasNext()) return output;Entity current = itemIterator.next();while (true) {&nbsp; &nbsp; output += current.toString();&nbsp; &nbsp; if (!itemIterator.hasNext()) break;&nbsp; &nbsp; Entity next = itemIterator.next();&nbsp; &nbsp; // ......&nbsp; &nbsp; current = next;}return output;你基本上跳过最后一个实体
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java