zoukankan      html  css  js  c++  java
  • [C#]想说一说嵌套数组

    今天早上,随感而发,随便写了点东西。结果下午的时候看了看评论,吓我一跳。估计是不是写代码的人看散文看得太少了,还是因为现在的人读的书太少了,似乎有有些大惊小怪。

    关于Y美女,我声明一下,尽管她很脱俗,不过情缘自有分定,我心里所爱的并不是Y美女,而是另外一位女孩子,境界也跟Y差不多。这样的女孩其实我遇过好几个个,要说喜欢,这几位林黛玉式的女孩我都比较喜欢,但是,爱和喜欢是两回事。

    看来,以后这些高级文章还是写到新浪博客好了,这个博客专用来写编程相关的。

    ------------------------------------------

    闲言少叙,说正经话。本文就说一说嵌套数组,估计又有人不满了,你也许会说:“这玩意儿平时都不曾用,说来干吗?” 如果只说平时用的,平时不用的就不说了,那就不是我了。我这个人很有趣,专挑别人不以为然的事情来说。

    先看看下面的代码,看了之后不要紧张。

     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" }
                                    }
                                }
                            }
                        }
                    }
                };


    怎么样?敢动手试试吗?不要看着害怕,其实很简单,我一口气就写下来了。还是那些规律。先看有多少对中括号,每进一层就减一对,一直减到只有一对中括号时,就是独立的数组元素了。另一点就是把大括号做好配对。

    我们也可以像盖房子一样,先把大致的框架弄好,然后再弄里面的各个房间,最后再从细节上装潢一下。

    比如,先这样。

                int[][][][] intArr = new int[][][][]
                {
    
                };

    然后这样。

                int[][][][] intArr = new int[][][][]
                {
                    new int[][][]
                    {
    
                    },
                    new int[][][]
                    {
    
                    },
                    new int[][][]
                    {
    
                    }
                }

    接着这样。

                int[][][][] intArr = new int[][][][]
                {
                    new int[][][]
                    {
                        new int[][]
                        {
    
                        }
                    },
                    new int[][][]
                    {
                        new int[][]
                        {
    
                        },
                        new int[][]
                        {
    
                        },
                        new int[][]
                        {
    
                        }
                    },
                    new int[][][]
                    {
                        new int[][]
                        {
    
                        }
                    }
                }

    最后填满元素。

                int[][][][] intArr = new int[][][][]
                {
                    new int[][][]
                    {
                        new int[][]
                        {
                            new int[] { 2 }
                        }
                    },
                    new int[][][]
                    {
                        new int[][]
                        {
                            new int[] { 28, 31 }
                        },
                        new int[][]
                        {
                            new int[] { 98 }
                        },
                        new int[][]
                        {
                            new int[] { 54 }
                        }
                    },
                    new int[][][]
                    {
                        new int[][]
                        {
                            new int[] { 8, 17, 36 },
                            new int[] { 16, 73 }
                        }
                    }
                };

    这个过程是很考验人的意志的,同时也可以检测一下你的心是否能够平静下来。只要你的心能静下来,哪怕是一连写上100层的嵌套数组也没问题的。

    当然,这仅仅是一种练习,真正做开发的话,极少这样做。平时练习编程的时候不妨试试,这不仅在锻炼你写代码的能力,也是锻炼你的心理素质。
    编程的时候,我很喜欢一边听无损音乐一边写代码。当然不是那种听着就心烦意乱的音乐,是那种很恬静很优雅,并且还夹带着大自然的声音的音乐,如泉水声,风声,鸟鸣声等。

  • 相关阅读:
    SQL Server创建复合索引时,复合索引列顺序对查询的性能影响
    SQL 查询性能优化----解决书签查找
    从源码分析 Spring 基于注解的事务
    jQuery最佳实践(不断更新中...)
    Java 8 LongAdders:管理并发计数器的正确方式
    Java中的显示锁 ReentrantLock 和 ReentrantReadWriteLock
    在IE8等不支持placeholder属性的浏览器中模拟placeholder效果
    颠覆式前端UI开发框架:React
    Whitecoin区块链钱包高级功能使用命令
    消息队列使用的四种场景介绍
  • 原文地址:https://www.cnblogs.com/tcjiaan/p/3530392.html
Copyright © 2011-2022 走看看