国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[C#]想說一說嵌套數組

2019-11-17 03:23:23
字體:
來源:轉載
供稿:網友

[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" }                                }                            }                        }                    },                    n
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛南县| 临邑县| 晋城| 隆安县| 岗巴县| 辉县市| 赣榆县| 安徽省| 安康市| 班玛县| 西宁市| 资源县| 开江县| 邓州市| 沛县| 耿马| 通化县| 永寿县| 胶州市| 武乡县| 霍山县| 台东市| 嵊泗县| 新化县| 惠来县| 民乐县| 海城市| 新郑市| 武平县| 南开区| 富蕴县| 亚东县| 牟定县| 南部县| 杭锦旗| 乐陵市| 阳信县| 大余县| 诸城市| 宝兴县| 铅山县|