【问题标题】:How am I getting an out-of-range exception here?我怎么会在这里得到一个超出范围的异常?
【发布时间】:2016-08-02 17:55:06
【问题描述】:

看了好久,没搞明白。正如下面的 cmets 所指出的,有问题的索引在列表中。

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

public class Mine
{
    public int Distance { get; set; }
    public int Gold { get; set; }
}

public class Move
{
    public int SourceIndex { get; set; }
    public int DestinationIndex { get; set; }
    public int Cost { get; set; }   
}

public class Program
{
    public static void Main()
    {

        var mines = new List<Mine> () {
            new Mine() { Distance = 10, Gold = 1 },
            new Mine() { Distance = 20, Gold = 2 },
            new Mine() { Distance = 25, Gold = 1 }
        };

        // Cost of consolidating the gold from mines[i1] to mines[i2]
        Func<int,int,int> Cost = (i1, i2) => Math.Abs(mines[i1].Distance - mines[i2].Distance) * mines[i1].Gold;

        // Number of mines to consolidate the gold into 
        int k = 1;


        var bestMove = new Move() { SourceIndex = -1, DestinationIndex = -1, Cost = Int32.MaxValue }; 

        // total cost
        int sum = 0;

        while(mines.Count != k)
        {
            var indices = Enumerable.Range(0, mines.Count).ToArray();
            for(int i = 0, j = 1; j < indices.Length; ++i, ++j)
            {
                int cost_ij = Cost(i,j);
                if(cost_ij < bestMove.Cost)
                {
                    bestMove.SourceIndex = i; 
                    bestMove.DestinationIndex = j; 
                    bestMove.Cost = cost_ij;
                }

                int cost_ji = Cost(j,i);
                if(cost_ji < bestMove.Cost)
                {
                    bestMove.SourceIndex = j; 
                    bestMove.DestinationIndex = i; 
                    bestMove.Cost = cost_ji;
                }
            }
            Console.WriteLine("bestMove.SourceIndex = {0}, bestMove.DestinationIndex = {1}", bestMove.SourceIndex, bestMove.DestinationIndex); // prints "bestMove.SourceIndex = 2, bestMove.DestinationIndex = 1"
            sum += bestMove.Cost;
            mines[bestMove.DestinationIndex].Gold += mines[bestMove.SourceIndex].Gold; // this is throwing an exception "Index was out of range. Must be non-negative and less than the size of the collection."
            mines.RemoveAt(bestMove.SourceIndex); 

        }
        Console.WriteLine(sum);
    }
}

小提琴:https://dotnetfiddle.net/hYa3A0

这是没有意义的,因为当时

            Console.WriteLine("bestMove.SourceIndex = {0}, bestMove.DestinationIndex = {1}", bestMove.SourceIndex, bestMove.DestinationIndex); // prints "bestMove.SourceIndex = 2, bestMove.DestinationIndex = 1"
            sum += bestMove.Cost;
            mines[bestMove.DestinationIndex].Gold += mines[bestMove.SourceIndex].Gold; // this is throwing an exception "Index was out of range. Must be non-negative and less than the size of the collection."
            mines.RemoveAt(bestMove.SourceIndex); 

第一次运行行,

bestMove.DestinationIndex = 2
bestMove.DestinationIndex = 1

mines.Count = 3

也许我只是疯了。

【问题讨论】:

  • 这个问题和这个问题惊人地相似:stackoverflow.com/questions/38711479/…这是一个作业吗?
  • 您使用的是相同的 bestMove 实例,您需要在每次循环后重置其字段(我猜)。
  • 该行执行时bestMove.DestinationIndexbestMove.SourceIndex 的值是多少?这将告诉您为什么会收到异常。
  • 我运行代码时,出现异常时bestMove.SourceIndex的值为2。当时,mines 列表中只有 2 个实例,因此唯一有效的索引是 0 和 1。2 显然超出了范围。

标签: c# .net algorithm data-structures


【解决方案1】:

indices 是从零开始的索引。

将 for 循环更改为:

for(int i = 0, j = 1; j < indices.Length-1; ++i, ++j)

【讨论】:

    【解决方案2】:

    问题在于您的计数器 j。 j 在某一点被分配 4,这超出了您的地雷集合的大小(3)。

    将while循环的第一行改成这样:

    var indices = Enumerable.Range(0, mines.Count - 1).ToArray();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      相关资源
      最近更新 更多