C#-创建一个包含值的整数索引的3D列表(类似于数组)

C#-创建一个包含值的整数索引的3D列表(类似于数组),c#,arrays,list,multidimensional-array,position,C#,Arrays,List,Multidimensional Array,Position,基本上,我想做的是在某个3D位置存储一个值,并能够访问它。例如,我将3的值存储在位置(4,5,6),然后我会告诉我的程序调用(4,5,6)的值,它将返回一个3 我相信(但可能是非常错误的)我会从以下内容开始创建列表: public static IList<IList<IList<int>>> valueList = new List<List<List<int>>>(); public静态IList valueList=

基本上,我想做的是在某个3D位置存储一个值,并能够访问它。例如,我将3的值存储在位置(4,5,6),然后我会告诉我的程序调用(4,5,6)的值,它将返回一个3

我相信(但可能是非常错误的)我会从以下内容开始创建列表:

public static IList<IList<IList<int>>> valueList = new List<List<List<int>>>();
public静态IList valueList=new List();
但我不知道如何存储或访问列表中的项目

我想要类似于数组的东西,但我不想使用数组,因为位置有可能是负数,并且列表中不应该有最小或最大位置


请让我知道,如果你知道如何做到这一点,或者你知道实现这一目标的另一种方法,因为我对我正在做的事情知之甚少。谢谢。

听起来你在为三维空间建模。可以创建一个表示三维坐标的类。可以使用此类的实例作为字典中的键来存储对象。简化的起点可能如下所示:

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);
    }
}
使用系统;
使用System.Linq;
使用System.Collections.Generic;
公共类向量
{
公共整数X{get;set;}
公共整数Y{get;set;}
公共int Z{get;set;}
}
公共课程
{
公共静态void Main()
{
字典映射=新字典();
var vector=new vector(){X=4,Y=5,Z=6};
map.Add(向量,3);
int结果=0;
if(map.Keys.Contains(vector))
{
结果=映射[向量];
}
控制台写入线(结果);
}
}

在使用字典之前,您需要检查字典是否包含您的密钥。您可能希望使用double而不是int。您可能需要重写相等运算符和哈希函数来比较等效向量。

听起来像是在为三维空间建模。可以创建一个表示三维坐标的类。可以使用此类的实例作为字典中的键来存储对象。简化的起点可能如下所示:

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);
    }
}
使用系统;
使用System.Linq;
使用System.Collections.Generic;
公共类向量
{
公共整数X{get;set;}
公共整数Y{get;set;}
公共int Z{get;set;}
}
公共课程
{
公共静态void Main()
{
字典映射=新字典();
var vector=new vector(){X=4,Y=5,Z=6};
map.Add(向量,3);
int结果=0;
if(map.Keys.Contains(vector))
{
结果=映射[向量];
}
控制台写入线(结果);
}
}

在使用字典之前,您需要检查字典是否包含您的密钥。您可能希望使用double而不是int。您可能需要重写相等运算符和哈希函数来比较等效向量。

如果列表中有很多项,这不会运行得很慢吗?我预计可能会有几十亿个项目(事情很快就会累加起来,一个1000x1000x1000的立方体就是10亿个项目)。@AaronFranke认为在内存中存储10亿个项目是不可能的,你最好重新考虑一下你的整个方法。谢谢你,这正是我需要的,我决定不惜任何代价避免数十亿的事情。我已经将我的网格划分为一个16x16x16块的字典,以分配处理所有网格所需的时间。我甚至将坐标限制为
short
s,而不是
int
s来限制大小。我最终调用了类Point3,它创建的类似于Unity的Vector3。我可能会添加完Vector3的所有方法,并在某一天发布它。如果列表中有很多项,这不会运行得很慢吗?我预计可能会有几十亿个项目(事情很快就会累加起来,一个1000x1000x1000的立方体就是10亿个项目)。@AaronFranke认为在内存中存储10亿个项目是不可能的,你最好重新考虑一下你的整个方法。谢谢你,这正是我需要的,我决定不惜任何代价避免数十亿的事情。我已经将我的网格划分为一个16x16x16块的字典,以分配处理所有网格所需的时间。我甚至将坐标限制为
short
s,而不是
int
s来限制大小。我最终调用了类Point3,它创建的类似于Unity的Vector3。我可能会添加完Vector3的所有方法,并在某一天发布它。