一點思考|工作十幾年了,竟從未用過do-while!

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最近在看 Java 的基礎知識,其中有部分是關於循環的,在 Java 中,循環的語法總共分爲 3 種: "},{"type":"codeinline","content":[{"type":"text","text":"for"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"while"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":",如下圖所示:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/40/4014dd8484d3a1de98d6b44262f2adcb.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但我驚奇的發現,在之前的職業生涯中(11 年),竟從未用過 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":"(尷尬),於是問了羣裏的小夥伴,發現也是鮮有人用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/09/09a77f277926ebff723aea942a9d0c49.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"do-while 語法分析"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們先來了解一下 do-while 的語法:"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"do {\n // statements\n} while (expression);"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:"},{"type":"text","marks":[{"type":"strong"}],"text":"最後一個冒號不能省略,否則會提示編譯出錯"},{"type":"text","text":"。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它的執行流程如下圖所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dc/dc56034fd1533d359090da86787755ad.png","alt":null,"title":"","style":[{"key":"width","value":"25%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那它究竟在什麼地方用呢?"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"do-while 使用場景"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在我多次的搜索和請教下,終於找到了兩個相對滿意的使用場景,接下來一起來看。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"使用場景一:搶票"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於搶票業務來說,無論三七二十一,先搶了再說,然後在判斷是否搶票成功,如果搶票成功則退出循環,否則繼續執行搶票,實現的僞代碼如下所示:"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"do {\n // 搶票代碼...\n} while (沒搶到票);"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"思路提供者:賈總"}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"使用場景二:進制轉換"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"經過大量搜索發現,在 JDK 的源碼中,也存在少量使用 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":" 的場景,比如 "},{"type":"codeinline","content":[{"type":"text","text":"Integer"}]},{"type":"text","text":" 中進制轉換,相關源碼如下:"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {\n int charPos = len;\n int radix = 1 << shift;\n int mask = radix - 1;\n do {\n buf[offset + --charPos] = Integer.digits[val & mask];\n val >>>= shift;\n } while (val != 0 && charPos > 0);\n\n return charPos;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如十進制轉二進制就會執行此方法,在進制轉換的業務中,無論任何情況,一定會至少執行一次進制轉換的,因此這種業務場景就非常適合 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":"。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"孔子說:溫故而知新。當我們學完很多知識之後,回過頭來再琢磨這些知識,發現很有趣,這就是知識的一大樂趣吧。本文我們介紹了兩種 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":" 的使用場景,搶票和進制轉換,你還知道哪些 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":" 的使用場景嗎?歡迎評論區告訴我~"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章