【问题标题】:Which index is the Z index in a 3D array哪个索引是 3D 数组中的 Z 索引
【发布时间】:2016-10-20 10:11:09
【问题描述】:

我无法理解 3D 数组的正确用法。我正在使用 C#。我的主要困惑是哪个索引是 Z 索引。不幸的是,我做了很多谷歌搜索,但我能找到的大部分信息都是关于二维数组的。然而Youtube上的这个家伙却说第三个索引是Z:https://www.youtube.com/watch?v=Eqo552n1DSc

根据该教程,在以下情况下:

  public string[,,] array3DString = new string[2, 5, 6];

2 = x、5 = y 和 6 = z。

起初这是有道理的,但后来我尝试通过循环来填充数组以给自己一个视觉理解:它帮助我直观地看待事物。所以我写了这个简单的程序:

class Test
    { 
        public string[,,] array3DString = new string[2, 5, 5];
        public void SetArray3DString()
        {
            for (int zIndex = 0; zIndex < 2; zIndex++)
            {               
                for (int r = 0; r < 5; r++)
                {
                    for (int c = 0; c < 5; c++)
                    {
                        array3DString[zIndex, r, c] = "z" + zIndex + "r" + r + "c" + c;                   
                    }
                }
            }
        }      
        public void PrintArray3DString()
        {
            for (int zIndex = 0; zIndex < 2; zIndex++)
            {
                Console.WriteLine("Z Index - " + zIndex);
                for (int r = 0; r < 5; r++)
                {
                    for (int c = 0; c < 5; c++)
                    {
                        if (c < 4)
                        {
                            Console.Write(array3DString[zIndex, c, r] + ",");
                        }
                        else
                        {
                            Console.WriteLine(array3DString[zIndex, c, r]);
                        }
                    }
                }
            }
        }

    }

    class Program
    {
        static void Main(string[] args)
        {            
            Test newTest = new Test(); 
            newTest.SetArray3DString();
            newTest.PrintArray3DString();
            while (true);
        }
    }

在我看来,当我尝试使用循环遍历 3D 数组时,将第一个索引设为 Z 更有意义,因为当然,外部循环是最后一个被索引的循环。输出是这样的:

Z Index - 0
z0r0c0,z0r1c0,z0r2c0,z0r3c0,z0r4c0
z0r0c1,z0r1c1,z0r2c1,z0r3c1,z0r4c1
z0r0c2,z0r1c2,z0r2c2,z0r3c2,z0r4c2
z0r0c3,z0r1c3,z0r2c3,z0r3c3,z0r4c3
z0r0c4,z0r1c4,z0r2c4,z0r3c4,z0r4c4
Z Index - 1
z1r0c0,z1r1c0,z1r2c0,z1r3c0,z1r4c0
z1r0c1,z1r1c1,z1r2c1,z1r3c1,z1r4c1
z1r0c2,z1r1c2,z1r2c2,z1r3c2,z1r4c2
z1r0c3,z1r1c3,z1r2c3,z1r3c3,z1r4c3
z1r0c4,z1r1c4,z1r2c4,z1r3c4,z1r4c4

这是我希望看到的。当我尝试制作最后一个索引 Z 时,我只是把自己弄糊涂了。

我是对的,还是 Youtube 视频中的那个人是对的?哪个索引应该是 Z,为什么?这还重要吗?

作为参考,我也试过这个网站:https://www.tutorialspoint.com/csharp/csharp_multi_dimensional_arrays.htm 但同样他们主要关注二维数组,并没有为我回答这个简单的问题。

提前感谢您的帮助。

【问题讨论】:

    标签: c# arrays multidimensional-array z-index


    【解决方案1】:

    没有关于如何存储它的规则。都是关于它是如何在内存中序列化的。

    您的代码在填充和打印时有所不同:

    填充:

    array3DString[zIndex, r, c]    <--- r = 2nd index..
    

    印刷:

    array3DString[zIndex, c, r]    <--- r = 3rd index..
    

    所以只要 c 和 r 的维度相等,这不会崩溃

    【讨论】:

    • 所以你是说我认为哪个 Z 索引并不重要?
    • 是的,我是这样打印出来的,这样在视觉上看起来是正确的。为及时回复干杯!
    • 我个人会为此避免使用多维数组。我宁愿使用一维数组。如果我想计算我使用的偏移量:((width*height) * z) + (width * y) + x 但我会做线性扫描。
    • 哦,好吧,我认为哪个是 Z 会有所不同,我很高兴它没有。我将为游戏设计一个关卡编辑器,所以我希望每个碰撞层,每个,地面/背景,玩家/障碍物都位于不同的 z 层上,这就是我研究它的原因
    • 感谢您的回复 :)
    猜你喜欢
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    相关资源
    最近更新 更多