iterate through recursive objects

前端 未结 5 1666
孤独总比滥情好
孤独总比滥情好 2021-01-29 14:54

I am trying to iterate through a recursive object but Java has no support for this from what I know.

For example, given the object Item:

pub         


        
5条回答
  •  逝去的感伤
    2021-01-29 15:40

    I don't know what the exact goal of the function is, but you can always loop recursively through children.

    For example

     public void loopChildren(List items) {
         for (Item i : items) {
             System.out.println(i.uuid);
             loopChildren(i.children);           
         }       
     }
    

    It just keep looping until the end of the list. If a child has no children List should be empty so it terminates for that iteration.

    Hope this helps.

提交回复
热议问题