1

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 :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

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)

2 Answers 2

1

The error is pretty clear actually. You are trying to remove an item from tlist at 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 tlist matches if (e2.s == e1.s && e2.d == e1.d), so you end up with a +1 over the actual index of the tlist array.

To elaborate further, let's assume for simplicity that tlist has 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 ind to 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

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.

Sign up to request clarification or add additional context in comments.

3 Comments

But in the above code I found the certain index on searching from tlist. How come it doesnot have value?
@ErBnAcharya I updated the answer with extra elaboration and what will fix the code.
@ErBnAcharya That is your prerogative, but the way stack overflow works is that you ask a question (in this case, why you were getting the error), and we answer it. So, I answered correctly as to why you are getting an index out of range error. If you have another question beyond that, you should open up a new question. But, that is just how I have seen things run just about every time...
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.