[C#]想说一说嵌套数组

闲言少叙,说正经话。本文就说一说嵌套数组,估计又有人不满了,你也许会说:“这玩意儿平时都不曾用,说来干吗?” 如果只说平时用的,平时不用的就不说了,那就不是我了。我这个人很有趣,专挑别人不以为然的事情来说。
 
先看看下面的代码,看了之后不要紧张。
 
复制代码
 1             int[][][] arrs = new int[3][][];
 2             arrs[0] = new int[2][]
 3             {
 4                 new int[3] { 1, 2, 3 },
 5                 new int[1] { 4 }
 6             };
 7             arrs[1] = new int[][]
 8             {
 9                 new int[] { 5, 6 },
10                 new int[] { 7, 8, 9, 10 },
11                 new int[] { 11, 12, 13 }
12             };
13             arrs[2] = new int[][]
14             {
15                 new int[] { 14, 15, 16, 17, 18 }
16             };
复制代码
这就是所谓的嵌套数组,其实也没什么的,要理解的话也不那么复杂。无非就是数组里面套了数组。也就是说,一个数组里面(第一层),每个元素又是一个数组(第二层)……
就拿上面的int数组来说,里面套了三层,一般来说,最里面一层数组的元素就不是数组了,就应该是单个int数值了。所以,嵌套数组的最里层都是单个值作为元素的,不然这数组可就要无限地套下去了。
 
要知道以上代码中的数组是啥结构也不用画图,我们只要调试运行,在给数组赋值后停下来,从“局部变量”窗口中我们可以查看到数组的结构。如下图:
 
 
 
这样看,应该可以理解的。
 
现在,就回到代码中,我们看要如何声明,其实说起来也不难,和声明一般的数组一样,比如int[][]就是两层数组。但要注意的问题在于实例化的时候。
 
在实例化的时候,最外面一层数组可以指定大小,但里面套的数组则不可;里面的数组在下一层数组实例化时才可以指定大小。一句话总结:new哪一层数组就可以明确指定哪一层数组的大小。
 
例如,下面的new法是会报错的。
 
            byte[][][] arr= new byte[3][5][2];
要这样new才不会报错。
 
            byte[][][] arr= new byte[3][][];
因为这样声明new了最外层的byte数组,所以只能给这一层指定维数。我们继续,把整个数组new完,并赋值。
 
复制代码
            byte[][][] arr = new byte[3][][] //3个元素
            {
                /* [0] */new byte[2][] //2个元素
                        {
                            /* [0] */new byte[] { 0x20, 0x01, 0xFE },
                            /* [1] */new byte[] { 0xD2, 0xC6, 0x01, 0x22 }
                        },
                /* [1] */new byte[1][] //1个元素
                        {
                            /* [0] */new byte[] { 0x33, 0x4A }
                        },
                /* [2] */new byte[3][] //3个元素
                        {
                            /* [0] */new byte[] { 0x2e, 0x40 },
                            /* [1] */new byte[] { 0x52, 0xb2, 0x06 },
                            /* [2] */new byte[2] { 0x11, 0x21 }
                        }
            };
复制代码
怎么样?有何感想?带了注释应该容易看一些。由于越往里面数组的层数就递减,所以每进一层,就少一对中括号,第一层三对中括号,第二层两对中括号,第三层一对中括号,然后里面就是真正的单个byte值。
 
在写代码的时候,我们应该使用上面例子的排版方式,这样层次就分明,写的时候不容易写错,就和代码中大括号的层次一样,恰好,用来包含数组元素的也是一对大括号。有层次地缩进,就不容易写错。
 
下面我们来个更猛的。
 
复制代码
            string[][][][][][][] strArr = new string[][][][][][][]
            {
                new string[][][][][][]
                {
                    new string[][][][][]
                    {
                        new string[][][][]
                        {
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "ai", "gooo", "put" },
                                    new string[] { "san", "de" }
                                },
                                new string[][]
                                {
                                    new string[] { "ki", "chd" }
                                }
                            },
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "ga" },
                                    new string[] { "x", "y", "w", "h" },
                                    new string[] { "cc", "li" }
                                },
                                new string[][]
                                {
                                    new string[] { "su" },
                                    new string[] { "dou", "xx", "f" }
                                },
                                new string[][]
                                {
                                    new string[] { "z", "xoo", "ui" },
                                    new string[] { "gn", "sun", "tttt", "fan" },
                                    new string[] { "yin", "ci", "zh" },
                                    new string[] { "oo", "yi" }
                                }
                            }
                        },
                        new string[][][][]
                        {
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "da" }
                                },
                                new string[][]
                                {
                                    new string[] { "00" }
                                }
                            },
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "xi", "ix" },
                                    new string[] { "ch" }
                                }
                            }
                        }
                    },
                    new string[][][][][]
                    {
                        new string[][][][]
                        {
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "zz", "ee" },
                                    new string[] { "teng" }
                                }
                            },
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "shi", "me", "ea" }
                                },
                                new string[][]
                                {
                                    new string[] { "ut" }
                                }
                            }
                        }
                    }
                },
                new string[][][][][][]
                {
                    new string[][][][][]
                    {
                        new string[][][][]
                        {
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "dd", "eaood" }
                                },
                                new string[][]
                                {
                                    new string[] { "ss", "48" },
                                    new string[] { "ha", "tie" }
                                }
                            }
                        },
                        new string[][][][]
                        {
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "tian" }
                                }
                            }
                        },
                        new string[][][][]
                        {
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "lan" }
                                },
                                new string[][]
                                {
                                    new string[] { "y", "zu" }
                                }
                            }
                        }
                    },
                    new string[][][][][]
                    {
                        new string[][][][]
                        {
                            new string[][][]
                            {
                                new string[][]
                                {
                                    new string[] { "hao", "zen", "oo", "du" },
                                    new string[] { "xi", "iow", "zzzzz" },
                                    new string[] { "hie", "zz", "8e" }
                                },
                                new string[][]
                                {
                                    new string[] { "fo" }
                                }
                            }
                        }
                    }
                }
            };
原文地址:https://www.cnblogs.com/ruishuang208/p/3890352.html