C# - 创建带值的整数索引的 3D 列表(类似于数组)
C# - Create a 3D list of integer index with values (similar to arrays)
基本上我想做的是有一种方法可以在某个 3D 位置存储一个值并能够访问它。因此,例如,我会将 3 的值存储在位置 (4, 5, 6),然后我稍后会告诉我的程序调用 (4, 5, 6) 处的值,它会 return a 3.
我相信(但可能是非常错误的)我会首先创建这样的列表:
public static IList<IList<IList<int>>> valueList = new List<List<List<int>>>();
但我不知道如何存储或访问列表中的项目。
我想要类似于数组的东西,但我不认为我想使用数组,因为位置有可能是负数并且列表中不应该有最小或最大位置。
如果您知道如何执行此操作或知道实现此目标的替代方法,请告诉我,因为我对自己在做什么一无所知。谢谢
听起来你在建模三维 space。您可以创建一个代表 3D 坐标的 class。您可以使用此 class 的实例作为字典中的键来存储对象。简化的起点可能如下所示:
using System;
using System.Linq;
using System.Collections.Generic;
public class Vector
{
public int X {get; set;}
public int Y {get; set;}
public int Z {get; set;}
}
public class Program
{
public static void Main()
{
Dictionary<Vector, int> map = new Dictionary<Vector, int>();
var vector = new Vector() { X = 4, Y = 5, Z = 6 };
map.Add(vector, 3);
int result = 0;
if (map.Keys.Contains(vector))
{
result = map[vector];
}
Console.WriteLine(result);
}
}
您需要在使用前检查字典是否包含您的密钥。您可能想使用双精度而不是整数。您可能希望覆盖相等运算符和哈希函数来比较等效向量。
基本上我想做的是有一种方法可以在某个 3D 位置存储一个值并能够访问它。因此,例如,我会将 3 的值存储在位置 (4, 5, 6),然后我稍后会告诉我的程序调用 (4, 5, 6) 处的值,它会 return a 3.
我相信(但可能是非常错误的)我会首先创建这样的列表:
public static IList<IList<IList<int>>> valueList = new List<List<List<int>>>();
但我不知道如何存储或访问列表中的项目。
我想要类似于数组的东西,但我不认为我想使用数组,因为位置有可能是负数并且列表中不应该有最小或最大位置。
如果您知道如何执行此操作或知道实现此目标的替代方法,请告诉我,因为我对自己在做什么一无所知。谢谢
听起来你在建模三维 space。您可以创建一个代表 3D 坐标的 class。您可以使用此 class 的实例作为字典中的键来存储对象。简化的起点可能如下所示:
using System;
using System.Linq;
using System.Collections.Generic;
public class Vector
{
public int X {get; set;}
public int Y {get; set;}
public int Z {get; set;}
}
public class Program
{
public static void Main()
{
Dictionary<Vector, int> map = new Dictionary<Vector, int>();
var vector = new Vector() { X = 4, Y = 5, Z = 6 };
map.Add(vector, 3);
int result = 0;
if (map.Keys.Contains(vector))
{
result = map[vector];
}
Console.WriteLine(result);
}
}
您需要在使用前检查字典是否包含您的密钥。您可能想使用双精度而不是整数。您可能希望覆盖相等运算符和哈希函数来比较等效向量。