为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

哆啦的时光机 2021-07-09 14:11:29
我正在创建一个带有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- 方法;
查看完整描述

3 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

改变

return output;

return output + current;

您没有将最后一个元素附加到您的输出中。

此外,您Iterator::next在检查之前调用Iterator::hasNext,如果您有一个空的Bag,则会有一个NoSuchElementException.


查看完整回答
反对 回复 2021-07-23
?
万千封印

TA贡献1891条经验 获得超3个赞

在其他答案中已经批评了迭代器的使用;还应注意边界条件:0 或 1 个儿童。


public String toString() {

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

    if (!children.isEmpty()) {

        // Unfortunately because of the recursion the following

        // cannot be done.

        //String list = children.stream()

        //    .map(Entity::toString).collect(Collectors.join(", "));

        //output += list.replaceFirst(", ([^,]*)$", " and $1");


        // Iterating from the back reversed, allows an easy

        // discrimination of comma and "and."

        StringBuilder list = new StringBuilder();

        String separator = " and ";

        ListIterator<Entity> itemIterator = children.listIterator(children.size());

        while (itemIterator.hasPrevious()) {

            Entity entity = itemIterator.previous();

            list.insert(0, entity.toString());

            if (itemIterator.hasPrevious()) {

                list.insert(0, separator);

                separator = ", ";

            }

        }

        output += list.toString();

    }

    return output;

}


查看完整回答
反对 回复 2021-07-23
?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

你在 while 条件下有错误


if (!itemIterator.hasNext()) return output;

Entity current = itemIterator.next();


while (true) {

    output += current.toString();


    if (!itemIterator.hasNext()) break;


    Entity next = itemIterator.next();

    // ......

    current = next;

}

return output;

你基本上跳过最后一个实体


查看完整回答
反对 回复 2021-07-23
  • 3 回答
  • 0 关注
  • 143 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信