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

关于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. Unity插件使用总结

    移动文件夹位置会引起错误的插件:EasySave2.MaterialUI.Gamestrap UI.Beautify

  2. 0_MVC+EF+Autofac(dbfirst)轻型项目框架_基本框架

    前言 原来一直使用他人的开源项目框架,异常的定位会很麻烦,甚至不知道这个异常来自我的代码还是这个框架本身.他人的框架有一定的制约性,也有可能是我对那些框架并没深入了解,因为这些开源框架在网上也很难找到 ...

  3. [Leetcode] Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. nandaom

    this python</div><div><br></div><div>hahah</div><div> 来自为知 ...

  5. Shader实例:序列帧动画

    效果: 序列帧图片网上随便找的,质量不是很好,重点不是它,不要在意. 思路: 1.之前都是在一张面片上直接映射一张纹理,IN.uv的范围是0~1 现在要映射一张纹理上的一小块区域,就要用这块区域的uv ...

  6. css单位:em,rem解释

    em:所有浏览器都符合:1em=16px;1.具有继承性2.em的根元素是body,当设置了根元素的大小时,大小是定义的数字乘以根元素定义的大小值 rem:1rem=16pxrem不具有继承性,其根元 ...

  7. swfupload纠结bug总结

    上传控件传到客户端的信息在IE7下乱码: 服务端 HttpUtility.UrlEncode,客户端 decodeURIComponent 上传大文件报404错: 用fiddler截取发现提示: 最可 ...

  8. *HDU3398 数学

    String Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. 登陆数据库,界面一直保持正在登陆的状态,oracle使用界面无法登陆

    原因:关机时没有关闭oracle窗口. 导致在登陆数据库的时候,使用oracle的这个界面登陆时,界面一直保持''正在登陆''的状态,一旦点击就会卡住,使界面变得无法响应. 然后使用sqlplus仍然 ...

  10. Javascript判断两个日期是否相等

    大家一定遇到过这样的情况,有两个日期对象,然后需要判断他们是否相等. 例如: var date1 = new Date("2013-11-29"); var date2 = new ...