超出索引例外

时间:2017-04-13 20:51:51

标签: c# arrays indexing

很抱歉,如果有一个简单的解决方案,一直试图找出它几个小时,我似乎无法找到代码的问题,它可能有什么问题?

 public static void ShortestPath(int[,] waypoint, int source, int verticesCount) 
        {
            source = 0;
            waypoint = new int[verticesCount, verticesCount];
            int[] distance = new int[verticesCount];
            int[] previous = new int[verticesCount];
            PriorityQueue<int> priorityqueue = new PriorityQueue<int>();

            for (int i = 0; i < verticesCount; i++)
            {
                for (int l = 0; l < verticesCount; l++)
                {
                    if (waypoint[i, l] != 0)
                    {
                        priorityqueue.Enqueue(i, waypoint[i, l]);
                    }
                }
            }

            while (!priorityqueue.empty() || priorityqueue != null)
            {
                int o = priorityqueue.dequeue_min();

                for (int v = 0; v < verticesCount; ++v)
                {
                    previous[v] = 0;
                    MinimumDistance(distance, verticesCount);     
                    if (waypoint[o, v] != 0)
                    {
                        if (distance[o] + waypoint[o, v] < distance[v])
                            distance[v] = distance[o] + waypoint[o, v];
                        previous[v] = o;
                        priorityqueue.Enqueue(o, distance[v]);

                    }
                      Print(distance[], verticesCount);
                }
            }

1 个答案:

答案 0 :(得分:0)

我认为您的问题与if循环中的while有关++v而不是v++

代码应该是:

...
for (int v = 0; v < verticesCount; v++)
...