繁体   English   中英

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

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

我正在尝试解决问题。 该程序包含图形中的所有边。 从源到目的地的最短路径是找出答案。 我有一个名为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");

        }


    }

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

未处理的异常: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.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.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.List 1 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.List [ch_2_3_27.Solution + edge] .ch_2_3_27处:0处的RemoveAt(Int32索引)[0x00000] .ch_2_3_27.Solution处:0处的[0x00000] [0x00000] .Main(System.String [] args)[0x00000] in:0

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

上面的Edge(edge)是一个结构,成员s,d,w(源,目标,权重全部为Int 32)

该错误实际上很明显。 您正在尝试从特定索引的tlist中删除项目。 但是,该索引没有值。

如果我猜到了,我想说的是,只有在您的tlist中没有匹配的条件为(e2.s == e1.s && e2.d == e1.d) ,才会发生这种情况,因此您最终在+1上tlist数组的实际索引。

为了进一步详细说明,为简单起见,假设tlist有1个项目,则使用该项目的索引将为0。如果您的if不起作用,则将ind++设置为1,因此将ind设置为1。索引为1,则会出现错误,因为0索引中只有一个对象,而1索引中没有任何对象

我将代码更改为更多类似这样的内容

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

我想说的是,如果在if内部执行RemoveAt,则会导致修改后的集合异常,因此我认为这是最好的解决方案。

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM