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

今天早上,隨感而發,隨便寫了點東西。結果下午的時候看了看評論,嚇我一跳。估計是否是寫代碼的人看散文看得太少了,仍是由於如今的人讀的書太少了,彷佛有有些大驚小怪。編程

關於Y美女,我聲明一下,儘管她很脫俗,不過情緣自有分定,我內心所愛的並非Y美女,而是另一位女孩子,境界也跟Y差很少。這樣的女孩其實我遇過好幾個個,要說喜歡,這幾位林黛玉式的女孩我都比較喜歡,可是,愛和喜歡是兩回事。數組

看來,之後這些高級文章仍是寫到新浪博客好了,這個博客專用來寫編程相關的。框架

------------------------------------------ui

閒言少敘,說正經話。本文就說一說嵌套數組,估計又有人不滿了,你也許會說:「這玩意兒平時都未曾用,說來幹嘛?」 若是隻說平時用的,平時不用的就不說了,那就不是我了。我這我的頗有趣,專挑別人不覺得然的事情來講。spa

先看看下面的代碼,看了以後沒關係張。調試

 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數值了。因此,嵌套數組的最裏層都是單個值做爲元素的,否則這數組可就要無限地套下去了。code

要知道以上代碼中的數組是啥結構也不用畫圖,咱們只要調試運行,在給數組賦值後停下來,從「局部變量」窗口中咱們能夠查看到數組的結構。以下圖:blog

這樣看,應該能夠理解的。ci

如今,就回到代碼中,咱們看要如何聲明,其實提及來也不難,和聲明通常的數組同樣,好比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層的嵌套數組也沒問題的。

固然,這僅僅是一種練習,真正作開發的話,極少這樣作。平時練習編程的時候不妨試試,這不只在鍛鍊你寫代碼的能力,也是鍛鍊你的心理素質。
編程的時候,我很喜歡一邊聽無損音樂一邊寫代碼。固然不是那種聽着就心煩意亂的音樂,是那種很恬靜很優雅,而且還夾帶着大天然的聲音的音樂,如泉水聲,風聲,鳥鳴聲等。

相關文章
相關標籤/搜索