今天早上,隨感而發,隨便寫了點東西。結果下午的時候看了看評論,嚇我一跳。估計是不是寫代碼的人看散文看得太少了,還是因為現在的人讀的書太少了,似乎有有些大驚小怪。
關於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層的嵌套數組也沒問題的。
當然,這僅僅是一種練習,真正做開發的話,極少這樣做。平時練習編程的時候不妨試試,這不僅在鍛煉你寫代碼的能力,也是鍛煉你的心理素質。
編程的時候,我很喜歡一邊聽無損音樂一邊寫代碼。當然不是那種聽着就心煩意亂的音樂,是那種很恬靜很優雅,並且還夾帶着大自然的聲音的音樂,如泉水聲,風聲,鳥鳴聲等。