【发布时间】:2009-06-18 02:51:20
【问题描述】:
我想做一个非常简单的任务,不知何故导致程序崩溃。
我有一个包含在其中的数字列表,都是唯一的。过了某个数字,我想倒序。
示例 : 5, 16, 11, 3, 8, 4 -> 5, 16, 11, 4, 8, 3 当使用 3 作为轴心点时.
以下是我尝试过的众多方法之一。
private List<int> ShiftPath(List<int> oldPath, int shift)
{
List <int> newPath = new List<int>();
int counter = 0;
// Start reordering
// Forwards
while (oldPath[counter] != shift)
{
newPath.Add(oldPath[counter]);
counter++;
}
// Backwards
counter = oldPath.Count - 1;
while (oldPath[counter] != shift)
{
newPath.Add(oldPath[counter]);
counter--;
}
// New endpoint
newPath.Add(shift);
// Update
return newPath;
}
现在可以了。它可能不是最佳解决方案,但它确实有效。我已经使用这种方法很长一段时间了,但现在我已经达到了列表中的项目数量变得非常大(超过 6,000 个)的地步。最后,在尝试向 newPath 添加内容时,我得到了 StackOverFlowException。
我 100% 确定不存在像 VS 声称的无限循环。我尝试了其他方法,例如直接获取项目范围,for 和 foreach 循环而不是 while,最终都会崩溃。数据量似乎太大了。而且它只会变得更大(高达 20,000)。
证明(?):即使这样也会使程序抛出异常: List
知道是什么原因造成的/如何解决它吗?
-来自初学者。
【问题讨论】: