changeset 3:7ede12c9a2e9

fix iterator
author one
date Fri, 27 Aug 2010 22:25:48 +0900
parents ebf0e1a8c727
children 8c33fd63fea6
files src/treecms/proto/test/PreOrderTreeWalker.java
diffstat 1 files changed, 7 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/treecms/proto/test/PreOrderTreeWalker.java	Fri Aug 27 18:26:09 2010 +0900
+++ b/src/treecms/proto/test/PreOrderTreeWalker.java	Fri Aug 27 22:25:48 2010 +0900
@@ -17,6 +17,7 @@
 	class IteratorState implements Iterator<NodeAPI> {
 		LinkedList<LinkedList<NodeAPI>>stack = new LinkedList<LinkedList<NodeAPI>>();
 		LinkedList<NodeAPI>children;
+		NodeAPI next;
 		
 		IteratorState(NodeAPI root) {
 			children = new LinkedList<NodeAPI>(root.getChildList());
@@ -29,13 +30,15 @@
 				children = stack.getLast();
 				stack.removeLast();
 			}
+			next = children.get(0);
+			children.remove(0);
+			if (! next.getChildList().isEmpty()) {
+				stack.addLast(children);
+				children = new LinkedList<NodeAPI>(next.getChildList());
+			}
 			return true;
 		}
 		public NodeAPI next() {
-			NodeAPI next = children.get(0);
-			children.remove(0);
-			stack.addLast(children);
-			children = new LinkedList<NodeAPI>(next.getChildList());
 			return next;
 		}
 		public void remove() {