【问题标题】:System.StackOverflowException in mscorlib.dllmscorlib.dll 中的 System.StackOverflowException
【发布时间】:2016-03-01 14:55:46
【问题描述】:

我收到了

“System.StackOverflowException”类型的未处理异常 发生在 mscorlib.dll'中

运行以下代码时出错。我无法弄清楚我哪里出了问题以及如何纠正它。有人可以帮我解决这个问题吗?

代码应该通过整数列表进行过滤,给定整数的哪组匹配目标数字;整数组被放入一个列表中,然后该列表被放入另一个列表中,以便进程继续处理剩余的数字。

我们将不胜感激任何和所有帮助。谢谢

    List<List<int>> results = null;
    public void Listing(List<int> numbers)
    {
        int target = Convert.ToInt32(textBox15.Text);
        Solver solver = new Solver();
        results = solver.Solve(target, numbers.ToArray());
    }
    public class Solver
    {
        private List<List<int>> mResults;
        int t = 0;
        public List<List<int>> Solve(int goal, int[] elements)
        {
            int t = goal;
            mResults = new List<List<int>>();
            RecursiveSolve(goal, 0, new List<int>(), new List<int>(elements), 0);
            return mResults;
        }
        private void RecursiveSolve(int goal, int currentSum, List<int> included, List<int> notIncluded, int startIndex)
        {
            List<int> nextIncluded = new List<int>(included);
            List<int> nextNotIncluded = new List<int>(notIncluded);
            try
            {
                for (int index = startIndex; index < notIncluded.Count; index++)
                {
                    int nextValue = notIncluded[index];
                    if (currentSum + nextValue == t)
                    {
                        List<int> newResult = new List<int>(included);
                        newResult.Add(nextValue);
                        mResults.Add(newResult);
                        t = goal;
                    }
                    else if (currentSum + nextValue < t)
                    {
                        nextIncluded.Add(nextValue);
                        nextNotIncluded.Remove(nextValue);
                        RecursiveSolve(t, currentSum + nextValue, nextIncluded, nextNotIncluded, startIndex++);
                    }
                    else if (currentSum + nextValue > t)
                    {
                        nextNotIncluded.Remove(nextValue);
                        nextNotIncluded.Add(nextValue);
                        t = t - 50;
                        RecursiveSolve(t, currentSum + nextValue, nextIncluded, nextNotIncluded, startIndex++);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Message : " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

【问题讨论】:

  • 数学问题的提示:在 Visual Studio 中右键单击项目 -> 选择“属性”。在“属性”中选择“构建”,然后在最底部单击“高级”。在“高级”窗口中检查/标记“检查算术上溢/下溢”,保存然后尝试再次运行您的应用程序
  • 我不完全理解发生了什么,但请查看您的第三个 else 块:这是正确的:nextNotIncluded.Remove(nextValue); nextNotIncluded.Add(nextValue);?好像有什么不对劲。猜第一行应该是nextIncluded.Remove(nextValue) 而不是nextNotIncluded...
  • 感谢 Jasper,不幸的是,这一次并没有起到什么作用;我仍然收到错误消息。
  • Rene - 这是正确的,我希望将下一个值移动到列表的末尾,这样虽然这次不符合要求,但以后仍然可以使用。
  • 在 Visual Studio 2013 中单击顶部的“调试”,然后单击菜单中的“异常”。在“例外”窗口中单击“查找”并在列表中搜索 SO,然后选中/标记并再次运行

标签: c# integer unhandled-exception mscorlib


【解决方案1】:

我觉得这段代码很可疑。 “nextNotIncluded”列表并没有变小,所以你最终得到了无限递归。

 else if (currentSum + nextValue > t)
 {
     nextNotIncluded.Remove(nextValue); // ** Remove?
     nextNotIncluded.Add(nextValue);    // ** Add Back?
     t = t - 50;
     RecursiveSolve(t, currentSum + nextValue, nextIncluded, nextNotIncluded, startIndex++);
 }

你是这个意思吗?

 else if (currentSum + nextValue > t)
 {
     nextNotIncluded.Remove(nextValue); // remove from nextNotIncluded
     nextIncluded.Add(nextValue);       // add to nextIncluded
     t = t - 50;
     RecursiveSolve(t, currentSum + nextValue, nextIncluded, nextNotIncluded, startIndex++);
 }

编辑:啊哈,这是另一个问题。在您的 Solver 构造函数中,您没有设置成员变量 t。这可能无法解决实际的 SO 异常,但它会让您更接近。

public List<List<int>> Solve(int goal, int[] elements)
 {
      /*int*/ t = goal;

【讨论】:

  • 您好史蒂夫,感谢您的回复。原始代码在我希望它的工作方式上是正确的。如果下一个值将 currentsum 置于目标之上,则无法使用它,因此我想将其从列表中删除并重新添加到列表末尾,以便以后可以重用,以查看下一个值是否满足目标。
【解决方案2】:

你们对错误区域的看法是对的,但不是 100% 正确。

我找到了解决方案,感谢您的帮助。当我再次调用“RecursiveSolve”线程时没有将“nextValue”添加到“currentSum”时,问题似乎已得到纠正。

 else if (currentSum + nextValue > t)
                {
                    nextNotIncluded.Remove(nextValue);
                    nextNotIncluded.Add(nextValue);
                    t = t - 50;
                    RecursiveSolve(t, currentSum + nextValue, nextIncluded, nextNotIncluded, startIndex++);
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多