java.util.Sublist 抛出 StackOverFlowError

标签 java error-handling stack-overflow

我们在生产中偶尔会遇到与执行 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/

相关文章:

java - 初始化新对象时的StackOverflow

java - 字节码和目标代码有什么区别?

Java加密,重置IV可以提高性能吗?

javascript - 如何使用django从服务器向客户端发送错误消息并将其打印到控制台?

jquery - 如何查看 jQuery 错误?

java - 当我使用递归获得超过4个完全数JAVA时出错

c - 栈溢出异常终止

java - 使用 javaparser 获取方法语句

java - 如何将数据从android发送到服务器上的java应用程序

android - AdMob错误: “You must have AdActivity declared in AndroidManifest.xml with configChanges”