我们在生产中偶尔会遇到与执行 SubList 操作相关的 StackOverFlowError 错误。有没有人以前见过这样的事情并且知道是什么原因造成的?
这是触发错误的调用代码:
FacesContext context = FacesContext.getCurrentInstance();
String newViewID = context.getViewRoot().getViewId();
if (newViewID != null) {
if (breadCrumbs.contains(newViewID)) {
// Trims the list upon going back to allow for multiple back button requests.
// This is lightweight and not intended for a complex circular navigation.
breadCrumbs = breadCrumbs.subList(0, breadCrumbs.indexOf(newViewID) + 1);
} else {
breadCrumbs.add(newViewID);
}
}
结果:
Caused By: java.lang.StackOverflowError
at java.util.SubList$1.<init>(AbstractList.java:688)
at java.util.SubList.listIterator(AbstractList.java:687)
at java.util.SubList$1.<init>(AbstractList.java:688)
at java.util.SubList.listIterator(AbstractList.java:687)
...
最佳答案
subList() 方法返回一个由原始列表支持的 View 。
根据 javadoc:
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
您正在对列表进行结构更改,所以所有的赌注都没有了 -- 任何事情 都可能发生,包括无限递归,这似乎正在发生。
关于java.util.Sublist 抛出 StackOverFlowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3790171/