c# - 我怎么在这里得到一个超出范围的异常?

标签 c# .net algorithm data-structures

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

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);
    }
}

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

也许我只是疯了。

最佳答案

indices 是一个从零开始的索引。

将 for 循环更改为:

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

关于c# - 我怎么在这里得到一个超出范围的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38727373/

相关文章:

c# - 在 Sql Server Session 数据库中存储多个值的有效方法是什么?

java - jni4net 不兼容的类更改错误

c - 有没有办法让这个归并排序算法更加高效?

c# - 在控制台窗口中显示来自数据库的数据

c# - LinkLabel 无下划线 - Compact Framework

c# - 掌上电脑 : Draw control to bitmap

: All possible ways of splitting a set of elements into two sets? 的算法

c++ - 给定有向图和商店位置放置商店,以便从任何城市到最近商店的最大距离最小化

C# - 在上下文菜单之前触发 "right click"事件

c# - 如何在不使用 try catch 的情况下处理异常?