简体   繁体   English

为什么迭代列表会给我stackoverflow错误?

[英]Why iterating list gives me stackoverflow error?

I am getting Stackoverflow whenever below code is ran. 每当运行以下代码时,我都会得到Stackoverflow。 I am not able to understand why this is happening? 我不明白为什么会这样? And how we can fix it? 以及我们如何解决它?

  public boolean compare(List<Task> source, List<Task> actual) {
    return compareTasks(source, actual) && compare(actual, source);
  }

  public boolean compareTasks(List<Task> source, List<Task> actual) {
    matchList = new ArrayList<Task>();
    differList = new ArrayList<Task>();
    for (Task task : actual) {
      if (source.contains(task)) {
        matchList.add(task);
      } else {
        differList.add(task);
      }
    }
    return (differList.size() == 0) ? true : false;
  }

Below is the error: 下面是错误:

java.lang.StackOverflowError
    at java.lang.StringBuilder.append(StringBuilder.java:119)
    at com.user.test.Task.getKey(Task.java:209)
    at com.user.test.Task.equals(Task.java:220)
    at java.util.ArrayList.indexOf(ArrayList.java:216)
    at java.util.ArrayList.contains(ArrayList.java:199)
    at java.util.Collections$UnmodifiableCollection.contains(Collections.java:1000)

I believe the issue is in here: 我相信问题出在这里:

public boolean compare(List<Task> source, List<Task> actual) {
   return compareTasks(source, actual) && compare(actual, source);
}

Imagine you want to compare list A to list B. If you'll notice: 假设您想将列表A与列表B进行比较。如果您发现:

  • Calling compare(A, B) calls compare(B, A). 调用compare(A,B)会调用compare(B,A)。
  • Calling compare(B, A) calls compare(A, B). 调用compare(B,A)会调用compare(A,B)。
  • Calling compare(A, B) calls compare(B, A). 调用compare(A,B)会调用compare(B,A)。
  • Calling compare(B, A) calls compare(A, B). 调用compare(B,A)会调用compare(A,B)。
  • Calling compare(A, B) calls compare(B, A). 调用compare(A,B)会调用compare(B,A)。
  • Calling compare(B, A) calls compare(A, B). 调用compare(B,A)会调用compare(A,B)。
  • Calling compare(A, B) calls compare(B, A). 调用compare(A,B)会调用compare(B,A)。
  • Calling compare(B, A) calls compare(A, B). 调用compare(B,A)会调用compare(A,B)。
  • etc. 等等

This recursion never terminates, so it eventually triggers a stack overflow. 此递归永远不会终止,因此最终会触发堆栈溢出。

To fix this issue, consider rewriting this as 要解决此问题,请考虑将其重写为

public boolean compare(List<Task> source, List<Task> actual) {
   return compareTasks(source, actual) && compareTasks(actual, source); // call compareTasks, not compare.
}

You are calling compareTasks again in the method. 您在方法中再次调用compareTasks。 Change it to the following line: return compareTasksList(source, actual) && compareTasksList(actual, source); 将其更改为以下行: return compareTasksList(source, actual) && compareTasksList(actual, source);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当字长超过4时,为什么此代码会给出stackoverflow错误? - why does this code gives stackoverflow error when word length is beyond 4? 我需要一个程序来对整数数组进行交替排序,但是它给了我一个StackOverflow错误 - I needed a program to sort an integer Array alternating, but it gives me a StackOverflow Error 为什么这段代码会给我超时错误? - Why does this code gives me timeout error? 链接列表排序方法在递归期间给出了StackOverFlow错误,并比较了列表中的元素 - Linked List sort method gives StackOverFlow Error during recursion, and comparing elements in the list 为什么这段代码在java中发送给我Stackoverflow - why this code send me Stackoverflow in java XSL中的零宽度空间会产生stackoverflow错误 - Zero width space in XSL gives stackoverflow error CheckboxField在Blackberry Java应用程序中给出了StackOverflow错误 - CheckboxField gives StackOverflow Error in Blackberry java application 遍历列表给出了NullPointerException - iterating through list gives a NullPointerException 为什么此代码导致StackOverflow错误: - Why is this code resulting in StackOverflow error: 为什么此代码显示 StackOverflow 错误? - Why this code is showing StackOverflow error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM