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.
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.
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 :01[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
at ch_2_3_27.Solution.dotest () [0x00000] in :0
at ch_2_3_27.Solution.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)
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.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 thetlistarray.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 setind++, thus settingindto 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 indexI would change the code to something more like this
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.