本帖最后由 程振 于 2012-9-23 22:01 编辑
- import java.util.ArrayList;
- import java.util.List;
- // problem from Think in Java Fourth Edition.
- // about unintended recursion
- //
- // just repace one line can fixed it
- public class InfiniteRecursion {
- @Override
- public String toString() {
- // tip: this is the source of the problem
- return "InfiniteRecursion address: " + this +"\n";
- }
-
- // main method is correct, no need fix
- public static void main(String[] args) {
- List<InfiniteRecursion> v = new ArrayList<InfiniteRecursion>();
- for(int i=0;i<10;++i){
- v.add(new InfiniteRecursion());
- }
- System.out.println(v);
- }
-
- }
复制代码 |