从以int[]数组为值的字典中获取

本文关键字:字典 获取 int 数组 从以 | 更新日期: 2023-09-27 18:01:15

我有以下一段代码

public Dictionary<int[], string> worldMap = new Dictionary<int[], string>();
for (int x=0; x <= 10; x++) {
    for (int y=0; y <= 10; y++) {
        int[] cords = new int[]{x,y};
        worldMap.Add(cords, "empty");          
    }
}

如何从这本字典中获取值?

从以int[]数组为值的字典中获取

创建一个IEqualityComparer并使用它定义字典。你可以在下面的SO问题中找到示例代码:

作为Dictionary 键的整数数组

您可以对整个字典进行迭代。

foreach (var map in worldMap)
{
    Console.WriteLine(String.Join(", ", map.Key) + " - " + map.Value);
}

或者通过参考查找

Dictionary<int[], string> worldMap = new Dictionary<int[], string>();
for (int x = 0; x <= 10; x++)
{
    for (int y = 0; y <= 10; y++)
    {
        int[] cords = new int[] { x, y };
        worldMap.Add(cords, "empty");
        Console.WriteLine(worldMap[cords]);
    }
}

当您使用数组或类时,字典将使用对背景中对象的引用。这使得有些事情变得不可能。例如,如果您新建了另一个具有相同值的列表,则字典将抛出一个异常,表示找不到键。

Dictionary<int[], string> worldMap = new Dictionary<int[], string>();
for (int x = 0; x <= 10; x++)
{
    for (int y = 0; y <= 10; y++)
    {
        int[] cords = new int[] { x, y };
        worldMap.Add(cords, "empty");
        //This will cause an exception
        Console.WriteLine(worldMap[new int[] { x, y }]);
    }
}

请考虑使用System.Drawing.Point而不是int数组。一般来说,Dictionaries只有在它是完全相同的对象时才会匹配。即使是另一个具有相同值的对象也无法工作。

但有些数据类型(如Point(不同,它们实现了字典中使用键所需的所有哈希和相等性检查功能。

填充世界地图后,您可以获得如下密钥(您必须添加using System.Linq(:

List<int[]> coords = worldMap.Keys.ToList();

然后你可以从坐标List中获得任何值,如下所示(确保你使用的值在List的范围内(:

worldMap[coords[0]]

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
    public static void Main()
    {
        Dictionary<int[], string> worldMap = new Dictionary<int[], string>();
        for (int x = 0; x <= 10; x++)
        {
            for (int y = 0; y <= 10; y++)
            {
                int[] cords = new int[] { x, y };
                worldMap.Add(cords, String.Format("empty{0}_{1}", x, y));
            }
        }
        // This should have 121 coordinates from (0, 0) to (10, 10)
        List<int[]> coords = worldMap.Keys.ToList();
        Console.WriteLine(worldMap[coords[21]]);
    }
}

演示

使用int[]作为单词地图坐标是一个糟糕的选择,原因有很多,尤其是当你似乎只使用两个值时,我可以通过零、一、三或更多个值发送。

您最好创建自己的只读类来保存坐标。

试试这个类:

public sealed class WorldMapCoord : IEquatable<WorldMapCoord>
{
    private readonly int _X; 
    private readonly int _Y;
    public int X { get { return _X; } } 
    public int Y { get { return _Y; } }
    public WorldMapCoord(int X, int Y)
    {
        _X = X; 
        _Y = Y;    
    }
    public override bool Equals(object obj)
    {
        if (obj is WorldMapCoord)
                return Equals((WorldMapCoord)obj);
        return false;
    }
    public bool Equals(WorldMapCoord obj)
    {
        if (obj == null) return false;
        if (!EqualityComparer<int>.Default.Equals(_X, obj._X)) return false; 
        if (!EqualityComparer<int>.Default.Equals(_Y, obj._Y)) return false;    
        return true;
    }
    public override int GetHashCode()
    {
        int hash = 0;
        hash ^= EqualityComparer<int>.Default.GetHashCode(_X); 
        hash ^= EqualityComparer<int>.Default.GetHashCode(_Y);
        return hash;
    }
    public override string ToString()
    {
        return String.Format("{{ X = {0}, Y = {1} }}", _X, _Y);
    }
}

现在你的代码看起来是这样的:

public Dictionary<WorldMapCoord, string> worldMap = new Dictionary<WorldMapCoord, string>();
for (int x=0; x <= 10; x++)
{
    for (int y=0; y <= 10; y++)
    {
        worldMap.Add(new WorldMapCoord(x, y), "empty");          
    }
}

由于该类是只读的,并且它实现了GetHashCode&Equals您可以使用此代码从字典中检索值:

for (int x=0; x <= 10; x++)
{
    for (int y=0; y <= 10; y++)
    {
        string value = worldMap[new WorldMapCoord(x, y)];
    }
}