C# System.ArgumentOutOfRangeException:参数超出范围。最短路径算法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9900481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:08:16  来源:igfitidea点击:

System.ArgumentOutOfRangeException: Argument is out of range. error in a shortest path algorithm

c#shortest-path

提问by progrrammer

I am trying to solve a problem. This program contains all the edge in a graph. The shortest path from source to destination is to find out. I have function named dotest as below.

我正在努力解决一个问题。该程序包含图中的所有边。从源到目的地的最短路径是找出。我有一个名为 dotest 的函数,如下所示。

 public void dotest()
    {
        List<edge> tlist;
        Int32  x;
        setall();
        Int32 ind;
        foreach (edge e1 in alltest)
        {
            tlist = new List<edge>(alledge);

            ind = 0;
            foreach (edge e2 in tlist)
            {
                if (e2.s == e1.s && e2.d == e1.d)
                {
                    break;
                }
                ind++;


            }
            tlist.RemoveAt(ind);



            x=shortpath(tlist, start, destination);
            if (x != -1)
                Console.WriteLine("{0}", x);
            else
                Console.WriteLine("Infinity");

        }


    }

Describing the above code. The code already contains list of alledge(all the edge or path). I have got series of input that contains list of edge to cut off and I have to find the shortest path of the new updated edge list. I compiled my test case and some of test case worked. But for some test case it have error message as.

描述上面的代码。代码已包含 alledge(所有边或路径)列表。我有一系列包含要切断的边列表的输入,我必须找到新更新的边列表的最短路径。我编译了我的测试用例,并且一些测试用例有效。但是对于某些测试用例,它有错误消息。

Unhandled Exception: System.ArgumentOutOfRangeException: Argument is out of range. Parameter name: index at System.Collections.Generic.List1[ch_2_3_27.Solution+edge].RemoveAt (Int32 index) [0x00000] in :0 at ch_2_3_27.Solution.dotest () [0x00000] in :0 at ch_2_3_27.Solution.Main (System.String[] args) [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Argument is out of range. Parameter name: index at System.Collections.Generic.List1[ch_2_3_27.Solution+edge].RemoveAt (Int32 index) [0x00000] in :0 at ch_2_3_27.Solution.dotest () [0x00000] in :0 at ch_2_3_27.Solution.Main (System.String[] args) [0x00000] in :0

未处理的异常:System.ArgumentOutOfRangeException:参数超出范围。参数名称:System.Collections.Generic.List 1[ch_2_3_27.Solution+edge].RemoveAt (Int32 index) [0x00000] in :0 at ch_2_3_27.Solution.dotest () [0x00000] in :0 at ch_2_3_27.Solution.Main (System.String[] args) [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Argument is out of range. Parameter name: index at System.Collections.Generic.List1[ch_2_3_27.Solution+edge].RemoveAt (Int32 index) [0x00000] in ch_2_3_27.Solution.dotest () [0x00000] in :0 at ch_2_3_27.Solution_3 [0x00000] .Main (System.String[] args) [0x00000] in :0

I really cannot locate out error and I think all other parts works fine. Anybody can help??

我真的找不到错误,我认为所有其他部分都可以正常工作。有人可以帮忙吗??

And Edge(edge) above is a struct with members s,d,w(source, destination, weight all Int 32)

上面的 Edge(edge) 是一个结构体,成员为 s,d,w(source, destination, weight all Int 32)

采纳答案by Justin Pihony

The error is pretty clear actually. You are trying to remove an item from tlistat a certain index. However, that index does not have a value.

错误其实很清楚。您正在尝试从tlist某个索引中删除项目。但是,该索引没有值。

If I were to guess, I would say that this only happens whenever nothing in your tlistmatches if (e2.s == e1.s && e2.d == e1.d), so you end up with a +1 over the actual index of the tlistarray.

如果我猜的话,我会说这只会在你的tlist匹配项中没有任何内容时发生if (e2.s == e1.s && e2.d == e1.d),所以你最终会在tlist数组的实际索引上得到 +1 。

To elaborate further, let's assume for simplicity that tlisthas 1 item, then the index to use that item will be 0. If your if does not work, then you will set ind++, thus setting indto 1. When you try to remove from the index at 1, then you get your error because there is only an object in the 0 index, and nothing in the 1 index

为了进一步详细说明,为了简单起见,让我们假设tlist有 1 个项目,那么使用该项目的索引将为 0。如果您的 if 不起作用,那么您将设置ind++,从而设置ind为 1。当您尝试从索引中删除时1,然后你会得到你的错误,因为 0 索引中只有一个对象,而 1 索引中没有任何对象

I would change the code to something more like this

我会将代码更改为更像这样的内容

        ind = -1;
        foreach (edge e2 in tlist)
        {
            ind++;
            if (e2.s == e1.s && e2.d == e1.d)
            {
                break;
            }
        }
        if(ind != -1)
            tlist.RemoveAt(ind);

I would say to just do the RemoveAt inside of the if, however, that will result in a modified collection exception, so I believe this is the best solution.

我会说只在 if 内部执行 RemoveAt ,但是,这将导致修改后的集合异常,所以我相信这是最好的解决方案。

回答by tsells

I would suggest creating a new list of edge types and adding ones you want to remove in your calculations. Then once complete - iterate over your "delete" list and remove them from the base list. Also make sure you have IComparable so you can compare the objects and remove the correct one.

我建议创建一个新的边类型列表,并在计算中添加要删除的边类型。然后一旦完成 - 遍历您的“删除”列表并将它们从基本列表中删除。还要确保您有 IComparable 以便您可以比较对象并删除正确的对象。