I'm trying to build this simple tree:Multiverse|     |--- Universe    |    |--- Supercluster        |        |--- Galaxy            |            |--- Andromeda            |            |--- Milky wayI came up with this simple method which recursively walk a tree:/** * recursively walk a tree structure */ public static void recursive(TreeViewItem treeNode){        System.out.println(treeNode.getText());        Iterator<TreeViewItem> itr = treeNode.getItems().iterator();        while (itr.hasNext()) {            TreeViewItem item = (TreeViewItem) itr.next();            recursive(item);        }}Is there a better way or more elegant way to do this?
Building a simple tree structure
java;recursion;tree
You're visiting and printing the tree elements in a depth-first manner, so depthFirstPrint would be a much better name than recursive (which is always a terrible name for any recursive function).You didn't need this cast:TreeViewItem item = (TreeViewItem) itr.next();because you correctly declared the iterator as Iterator<TreeViewItem> itrAs of Java 1.5, you can use a for-each loop, which is simple and elegant:for (TreeViewItem item : treeNode.getItems()) {    depthFirstPrint(item);}