【发布时间】:2015-12-30 10:58:44
【问题描述】:
在学习自我参照集合时,我想出了它在哪里扔 堆栈溢出错误。
请在下面找到源代码。
import java.util.*;
public class TestSelfColl {
public static void main(final String[] args) {
test(new ArrayList<Collection<?>>());
test(new LinkedList<Collection<?>>());
test(new HashSet<Collection<?>>());
test(new LinkedHashSet<Collection<?>>());
}
private static void test(final Collection<Collection<?>> collection) {
collection.add(collection);
System.out.println(collection);
try {
System.out.println(collection.hashCode());
} catch (final StackOverflowError err) {
System.out.println(err + " for " + collection.getClass());
}
}
}
真的很想知道为什么会出现这个错误。
我的期望是我会得到输出:
[(此收藏)]
123
..
但相比之下我得到了..
[(此收藏)]
类 java.util.ArrayList 的 java.lang.StackOverflowError
...
【问题讨论】:
标签: java generics collections