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

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

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

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

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

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

             int[][][] arrs = new int[][][];
arrs[] = new int[][]
{
new int[] { , , },
new int[] { }
};
arrs[] = new int[][]
{
new int[] { , },
new int[] { , , , },
new int[] { , , }
};
arrs[] = new int[][]
{
new int[] { , , , , }
};

这就是所谓的嵌套数组,其实也没什么的,要理解的话也不那么复杂。无非就是数组里面套了数组。也就是说,一个数组里面(第一层),每个元素又是一个数组(第二层)……
就拿上面的int数组来说,里面套了三层,一般来说,最里面一层数组的元素就不是数组了,就应该是单个int数值了。所以,嵌套数组的最里层都是单个值作为元素的,不然这数组可就要无限地套下去了。

要知道以上代码中的数组是啥结构也不用画图,我们只要调试运行,在给数组赋值后停下来,从“局部变量”窗口中我们可以查看到数组的结构。如下图:

这样看,应该可以理解的。

现在,就回到代码中,我们看要如何声明,其实说起来也不难,和声明一般的数组一样,比如int[][]就是两层数组。但要注意的问题在于实例化的时候。

在实例化的时候,最外面一层数组可以指定大小,但里面套的数组则不可;里面的数组在下一层数组实例化时才可以指定大小。一句话总结:new哪一层数组就可以明确指定哪一层数组的大小。

例如,下面的new法是会报错的。

            byte[][][] arr= new byte[][][];

要这样new才不会报错。

            byte[][][] arr= new byte[][][];

因为这样声明new了最外层的byte数组,所以只能给这一层指定维数。我们继续,把整个数组new完,并赋值。

            byte[][][] arr = new byte[][][] //3个元素
{
/* [0] */new byte[][] //2个元素
{
/* [0] */new byte[] { 0x20, 0x01, 0xFE },
/* [1] */new byte[] { 0xD2, 0xC6, 0x01, 0x22 }
},
/* [1] */new byte[][] //1个元素
{
/* [0] */new byte[] { 0x33, 0x4A }
},
/* [2] */new byte[][] //3个元素
{
/* [0] */new byte[] { 0x2e, 0x40 },
/* [1] */new byte[] { 0x52, 0xb2, 0x06 },
/* [2] */new byte[] { 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[] { "" }
}
},
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", "" },
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[] { }
}
},
new int[][][]
{
new int[][]
{
new int[] { , }
},
new int[][]
{
new int[] { }
},
new int[][]
{
new int[] { }
}
},
new int[][][]
{
new int[][]
{
new int[] { , , },
new int[] { , }
}
}
};

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

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

[C#]想说一说嵌套数组的更多相关文章

  1. mongodb 更新嵌套数组的值

    概要 本文主要讲述在 mongodb 中,怎么更新嵌套数组的值. 使用$更新数组 基本语法  { "<array>.$" : value } 可以用于:update, ...

  2. C#多维数组与嵌套数组

    using System; namespace abc.e.f//等价于下面分层嵌套的写法.且这种写法不管命名空间abc有没有定义过,也不管命名空间e有没有定义过 { class MYTestX { ...

  3. HTML(DOM)与JavaScript嵌套数组之间相互转换

    html2ja:将html目标元素解析为JavaScript数组字面量,每项的值为tagName, className, id等CSS选择器组合: showJa:将html2ja生成的数组缩进格式化显 ...

  4. C# json反序列化 对象中嵌套数组 (转载) 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

    C# json反序列化 对象中嵌套数组 (转载)   看图: 这里可以看到是二层嵌套!!使用C#如何实现?? 思路:使用list集合实现 → 建立类 → list集合 → 微软的   Newtonso ...

  5. js处理复杂数据格式数组嵌套对象,对象嵌套数组,reduce处理数据格式

    let list=[ {id:1,name:'a'}, {id:1,name:'b'}, {id:1,name:'c'}, {id:2,name:'A'}, {id:2,name:'B'}, {id: ...

  6. C# .NET Core 3.1中使用 MongoDB.Driver 更新嵌套数组元素和关联的一些坑

    C# .NET Core 3.1中使用 MongoDB.Driver 更新数组元素和关联的一些坑 前言: 由于工作的原因,使用的数据库由原来的 关系型数据库 MySQL.SQL Server 变成了 ...

  7. MongoDB学习笔记~官方驱动嵌套数组对象的更新

    回到目录 对于数组对象mongodb本身是支持的,不过对于数组的更新,mongodb的Csharp驱动目前只支持一级,即你的对象里包含数组,而数组又包括数组,这表示两层,这在更新子数组时,Csharp ...

  8. Vue 嵌套数组 数组更新视图不更新

    关于Vue的响应式原理,可以看官方文档或其他资料, https://www.jianshu.com/p/34de360d6035 data里定义了一个数组arr,数组的元素可以是同样格式的数组arrC ...

  9. php输出json,需要嵌套数组和对象问题

    https://segmentfault.com/q/1010000009985295 $tmp = []; $tmp['id'] = 'aaa'; $tmp['name'] = 'bbb'; $tm ...

随机推荐

  1. 简述HTML DOM及其节点分类

    在JavaScript中,document这个对象大家一定很熟悉,哪怕是刚刚开始学习的新人,也会很快接触到这个对象.而document对象不仅仅是一个普通的JavaScript内置对象,它还是一个巨大 ...

  2. Win10系统怎样让打开图片方式为照片查看器

    转载自:百度经验 http://jingyan.baidu.com/article/5d368d1ef0cad13f60c057e3.html 1.首先,我们需要使用注册表编辑器来开启Win10系统照 ...

  3. java学习-关于字符串String

    有必要总结记录一下java的学习,否则,永远只是记忆碎片化和always google(费时) 刚好,小伙伴给了一份自己做的review,在学习的过程中,update一下自己的见解和学习内容: 关于S ...

  4. ie8中遇到的兼容问题以及解决方案

    一.CSS3 1.可以通过在css中引入pie.htc,处理兼容问题(可处理的属性) -webkit-box-shadow: 0 1px 5px #ff2826; -webkit-border-rad ...

  5. webform 光棒效果,删除操作弹出确定取消窗口

    鼠标移入onmouseover和鼠标移出onmouseout,代码里没大写我这也就不大写了.那首先,我们得获取Class为tr_item里的所有东西,也就是项标签里的数据.然后呢,我们定义一个oldC ...

  6. Struts 2的OGNL的根对象

    Struts2中的OGNL表达式语言是对Xwork的OGNL的封装.我们要理解一下几点: 1. Struts2中将ActionContext作为OGNL的上下文环境(ActionContext内部含有 ...

  7. socket编程学习step1

    socket学习参考链接,赞一个:http://blog.csdn.net/hguisu/article/details/7445768 sockets(套接字)编程有三种,流式套接字(SOCK_ST ...

  8. What's the difference between a stub and mock?

    I believe the biggest distinction is that a stub you have already written with predetermined behavio ...

  9. 用微信小程序开发的Canvas绘制可配置的转盘抽奖

    使用https://github.com/givebest/GB-canvas-turntable代码移植过而来. 其它 微信小程序感觉是个半成品,代码移植过程比较繁琐麻烦.canvas API 部分 ...

  10. Autocad 2012 win7(64位)启动时一直卡在acmgd.dll处的解决方案

    安装Autocad 2012后,激活成功后,无法正常启动,一直卡在加载acmgd.dll 通过Procmon监控后发现加载C:\Windows\fonts\AdobeFnt11.lst处出错, 通过命 ...