{"id":2227,"date":"2023-05-06T21:53:32","date_gmt":"2023-05-06T13:53:32","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2227"},"modified":"2023-05-06T21:53:42","modified_gmt":"2023-05-06T13:53:42","slug":"groovy-code-example-list","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/06\/groovy-code-example-list\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; list"},"content":{"rendered":"<h2>list\u5217\u8868\u5b9a\u4e49<\/h2>\n<pre><code class=\"language-java\">def name = &#039;John&#039;\ndef surname = &#039;Doe&#039;\n\/\/\u5b57\u7b26\u4e32\u62fc\u63a5\nprintln &#039;Hello &#039; + name + &#039; &#039; + surname \/\/ Hello John Doe\n\n\/\/GString\u7684\u4f7f\u7528\ndef name = &#039;John&#039;\ndef surname = &#039;Doe&#039;\n\/\/${varName} \u53d8\u91cf\u5360\u4f4d\u7b26\nprintln &quot;Hello ${name} ${surname}&quot; \/\/ Hello John Doe\nprintln &#039;Hellow ${name} ${surname}&#039; \/\/ Hellow ${name} ${surname}<\/code><\/pre>\n<h2>\u5217\u8868(list)\u6dfb\u52a0\u5bf9\u8c61<\/h2>\n<p>groovy\u652f\u6301\u591a\u79cd\u65b9\u5f0f\u5f80\u5217\u8868list\u4e2d\u6dfb\u52a0\u5bf9\u8c61\u3002<\/p>\n<ul>\n<li>1\uff09\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>push<\/code>\u548c<code>&lt;&lt;<\/code>\u65b9\u6cd5\u5f80\u5217\u8868list\u7684\u5c3e\u90e8\u6dfb\u52a0\u5bf9\u8c61<\/li>\n<li>2\uff09\u652f\u6301\u50cf\u6570\u7ec4\u90a3\u6837\u589e\u52a0\u5bf9\u8c61\uff0c\u5982<code>cartoons[3]=&#039;Batman&#039;<\/code><\/li>\n<li>3\uff09\u652f\u6301\u901a\u8fc7<code>plus<\/code>\u65b9\u6cd5\u589e\u52a0\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u628a\u8fd9\u4e2a\u65b9\u6cd5\u5f53\u505a\u662f\u8fde\u63a5\u5217\u8868<\/li>\n<li>4\uff09\u652f\u6301\u901a\u8fc7<code>add\/addAll<\/code>\u7684\u65b9\u6cd5\u5c06\u5bf9\u8c61\u52a0\u5165\u5230\u5217\u8868\u4e2d<\/li>\n<\/ul>\n<pre><code class=\"language-java\">def cartoons = [&#039;Regular Show&#039;, &#039;The Amazing World of Gumball&#039;]\ncartoons.push(&#039;Adventure Time&#039;)\ncartoons[3] = &#039;Batman&#039;\ncartoons &lt;&lt; &#039;Smurfs&#039;\n\nprintln cartoons \/\/ [Adventure Time, Regular Show, The Amazing World of Gumball, Batman, Smurfs]\n\ncartoons.plus(4, &#039;Scooby Doo&#039;) \/\/plus\u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u539f\u5217\u8868\u6570\u636e\nprintln cartoons \/\/ [Adventure Time, Regular Show, The Amazing World of Gumball, Batman, Smurfs]\n\ndef cartoonsWithPlus = cartoons.plus(2, &#039;Scooby Doo&#039;)\nprintln cartoonsWithPlus \/\/ [Adventure Time, Regular Show, Scooby Doo, The Amazing World of Gumball, Batman, Smurfs]\n\ncartoons.add(0, &#039;Johnny Test&#039;)\nprintln cartoons \/\/ [Johnny Test, Adventure Time, Regular Show, The Amazing World of Gumball, Batman, Smurfs]\n\ncartoons.addAll(2, [&#039;Tom and Jerry&#039;, &#039;Uncle Grandpa&#039;]) \/\/ [Johnny Test, Adventure Time, Tom and Jerry, Uncle Grandpa, Regular Show, The Amazing World of Gumball, Batman, Smurfs]\nprintln cartoons<\/code><\/pre>\n<h2>\u5217\u8868list\u4e2d\u5220\u9664\u5bf9\u8c61<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u6216\u8005\u662f<code>pop()<\/code>\u65b9\u6cd5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-java\">def cartoons = [&#039;Regular Show&#039;, &#039;The Amazing World of Gumball&#039;, &#039;Adventure Time&#039;]\ndef poppedElement = cartoons.pop()\nprintln poppedElement \/\/ Adventure Time\nprintln cartoons \/\/ [Regular Show, The Amazing World of Gumball]\n\ncartoons.remove(0)\nprintln cartoons \/\/ [The Amazing World of Gumball]<\/code><\/pre>\n<p>\u5f53\u4f7f\u7528pop\u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u603b\u662f\u5220\u9664\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528remove(index)\u5220\u9664\u6307\u5b9a\u4f4d\u7f6e\u7684\u5bf9\u8c61\u3002<\/p>\n<h2>\u5217\u8868list\u7684\u67e5\u627e<\/h2>\n<p>\u5f88\u591a\u65f6\u5019\u6211\u4eec\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u67e5\u627e\uff0c\u4ee5\u627e\u5230\u6211\u4eec\u6240\u9700\u8981\u7684\u5217\u8868\u9879\u3002\u5e86\u5e78\u7684\u662f\u8fd9\u5728groovy\u4e2d\u975e\u5e38\u5bb9\u6613\u5b9e\u73b0\u3002<\/p>\n<pre><code class=\"language-java\">def cartoons = [\n    &#039;Regular Show&#039;,\n    &#039;The Amazing World of Gumball&#039;,\n    &#039;Adventure Time&#039;,\n    &#039;Uncle Grandpa&#039;,\n    &#039;Batman&#039;,\n    &#039;Scooby Doo&#039;\n]\ndef scoobyDoo = cartoons.find { it == &#039;Scooby Doo&#039; }\nprintln scoobyDoo \/\/ Scooby Doo\n\ndef cartoonNamesWithSizeGreaterThan12 = cartoons.findAll { it.size() &gt; 12 }\nprintln cartoonNamesWithSizeGreaterThan12 \/\/ [The Amazing World of Gumball, Adventure Time, Uncle Grandpa]\n\ndef cartoonNamesWithSizeGreaterThan15 = cartoons.findAll { cartoon -&gt; cartoon.size() &gt; 15 }\nprintln cartoonNamesWithSizeGreaterThan15 \/\/ [The Amazing World of Gumball]\n\ndef cartoonsFoundWithRegex = cartoons.findAll { it =~ \/an\/ }\nprintln cartoonsFoundWithRegex \/\/ [Uncle Grandpa, Batman]\n\ndef cartoonIndexList = cartoons.findIndexOf { it =~ \/^A\/ }\nprintln cartoonIndexList \/\/ 2\n\ndef cartoonIndexListWithStartPoint = cartoons.findIndexOf(4) { it =~ \/^A\/ }\nprintln cartoonIndexListWithStartPoint \/\/ -1\n\ndef cartoonLastIndex = cartoons.findLastIndexOf { it.size() &gt; 10 }\nprintln cartoonLastIndex \/\/ 3<\/code><\/pre>\n<p>\u901a\u8fc7find\u65b9\u6cd5\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5b9e\u73b0\u5217\u8868\u7684\u67e5\u627e\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528it\u53c2\u6570\u6216\u662f\u81ea\u5b9a\u4e49\u53c2\u6570\u522b\u540d\uff0c\u7136\u540e\u5728\u95ed\u5305\u4e2d\u5b9a\u4e49\u9700\u8981\u67e5\u627e\u7684\u8868\u8fbe\u5f0f\u3002find\u65b9\u6cd5\u4f1a\u8fd4\u56de\u9996\u4e2a\u5339\u914d\u7684\u5217\u8868\u9879\uff0c\u5982\u679c\u4f60\u60f3\u67e5\u627e\u6240\u6709\u5339\u914d\u7684\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>findAll<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7b26\u5408\u67e5\u627e\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u5217\u8868\u3002<\/p>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u4ec5\u4ec5\u60f3\u67e5\u627e\u5339\u914d\u9879\u7684\u4f4d\u7f6e\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u4f7f\u7528<code>findIndexOf<\/code>\uff0c\u8be5\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u652f\u6301\u8d77\u59cb\u4f4d\u7f6e\uff0c\u5f53\u4f60\u6307\u5b9a\u4e86\u8d77\u59cb\u4f4d\u7f6e\uff0c\u65b9\u6cd5\u5c06\u4ece\u8be5\u4f4d\u7f6e\u5f00\u59cb\u67e5\u627e\u5339\u914d\u7684\u5217\u8868\u9879\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u67e5\u627e\u7b26\u5408\u6761\u4ef6\u7684\u6700\u540e\u4e00\u4e2a\u5217\u8868\u9879\uff0c\u53ef\u4ee5\u4f7f\u7528<code>findLastIndexOf<\/code>\u65b9\u6cd5\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4f4d\u7f6e\u3002<\/p>\n<h2>\u5217\u8868(list)\u7684\u5206\u7ec4\u5408\u5e76(collate)<\/h2>\n<p>\u793a\u4f8b\u5982\u4f55\u4f7f\u7528groovy\u5c06\u5b8c\u6574\u7684\u5217\u8868\u5982\u4f55\u5206\u5272\u6210\u4e3a\u5b50\u5217\u8868\u3002<\/p>\n<pre><code class=\"language-java\">def cartoons = [\n        &#039;Regular Show&#039;,\n        &#039;The Amazing World of Gumball&#039;,\n        &#039;Adventure Time&#039;,\n        &#039;Uncle Grandpa&#039;,\n        &#039;Batman&#039;\n    ]\n\n\/\/\u4e24\u4e24\u5408\u5e76\ndef cartoonsSplitListWithTwoCartoonEach = cartoons.collate(2)\nprintln cartoonsSplitListWithTwoCartoonEach \/\/ [[Regular Show, The Amazing World of Gumball], [Adventure Time, Uncle Grandpa], [Batman]]\n\n\/\/\u4e09\u4e2a\u4e00\u7ec4\u5408\u5e76\ndef cartoonsSplitListWithThreeCartoonEach = cartoons.collate(3)\nprintln cartoonsSplitListWithThreeCartoonEach \/\/ [[Regular Show, The Amazing World of Gumball, Adventure Time], [Uncle Grandpa, Batman]]\n\n\/\/\u4e24\u4e2a\u4e00\u7ec4\u5408\u5e76\uff0c\u820d\u5f03\u5b64\u7acb\u4e0d\u6210\u7ec4\u7684\u5143\u7d20\ndef cartoonsSplitListWithoutRemainder = cartoons.collate(2, false)\nprintln cartoonsSplitListWithoutRemainder \/\/ [[Regular Show, The Amazing World of Gumball], [Adventure Time, Uncle Grandpa]]<\/code><\/pre>\n<p>groovy\u53ea\u662f\u5c06\u5217\u8868\u901a\u8fc7\u5206\u7ec4\u5408\u5e76\u7684\u65b9\u5f0f\u5206\u5272\u6210\u4e3a\u591a\u4e2a\u5b50\u5217\u8868\u3002collate\u7684\u65b9\u6cd5\u652f\u6301\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u5b50\u5217\u8868\uff08\u5206\u7ec4\uff09\u7684\u5927\u5c0f\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u660e\u5269\u4e0b\u5143\u7d20\uff08\u672a\u80fd\u5206\u7ec4\u7684\u3001\u5b64\u7acb\u7684\uff09\u662f\u5426\u4fdd\u7559\u3002<\/p>\n<h2>\u5217\u8868\u5143\u7d20\u7684\u4e2a\u6570\u7edf\u8ba1(count)<\/h2>\n<p>\u5982\u679c\u60f3\u8981\u77e5\u9053\u5217\u8868\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u901a\u8fc7size()\u65b9\u6cd5\u83b7\u53d6\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u7edf\u8ba1\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528count\u65b9\u6cd5\u83b7\u53d6\u3002<\/p>\n<pre><code class=\"language-java\">def cartoons = [\n        &#039;Regular Show&#039;,\n        &#039;The Amazing World of Gumball&#039;,\n        &#039;Adventure Time&#039;,\n        &#039;Regular Show&#039;,\n        &#039;Adventure Time&#039;,\n        &#039;Adventure Time&#039;\n    ]\n\/\/\u8ba1\u7b97\u5217\u8868\u7684\u5927\u5c0f\ndef cartoonCount = cartoons.size()\nprintln cartoonCount \/\/ 6\n\n\/\/\u7edf\u8ba1&#039;Adventure Time&#039;\u51fa\u73b0\u6b21\u6570\ndef advTimeCount = cartoons.count(&#039;Adventure Time&#039;)\nprintln advTimeCount \/\/ 3\n\n\/\/\u7edf\u8ba1&#039;Regular Show&#039;\u51fa\u73b0\u6b21\u6570\ndef regShowCount = cartoons.count(&#039;Regular Show&#039;)\nprintln regShowCount \/\/ 2<\/code><\/pre>\n<h2>\u5217\u8868\u5143\u7d20\u7684\u64cd\u4f5c<\/h2>\n<p>\u5982\u679c\u4f60\u76f8\u5bf9\u5217\u8868\u4e2d\u5143\u7d20\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\uff0cgroovy\u5c31\u4e3a\u4e86\u63d0\u4f9b\u4e86\u5f88\u65b9\u4fbf\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u5982\u4f60\u60f3\u628a\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u5b57\u7b26\u8f6c\u4e3a\u5927\u5199\u6216\u8005\u4ec5\u4ec5\u8ba9A\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u5927\u5199\u3002<\/p>\n<pre><code class=\"language-java\">def cartoons = [\n        &#039;Regular Show&#039;,\n        &#039;The Amazing World of Gumball&#039;,\n        &#039;Adventure Time&#039;\n]\n\n\/\/\u5b9a\u4e49\u53ea\u628aA\u5f00\u5934\u7684\u5b57\u7b26\u4e32\u5927\u5199\ndef uc(String cartoon) {\n    if (cartoon =~ \/^A\/ ) cartoon.toUpperCase()\n    else cartoon\n}\n\n\/\/\u5168\u90e8\u5927\u5199\ndef cartoonsWithUpperCase = cartoons*.toUpperCase()\nprintln cartoonsWithUpperCase \/\/ [REGULAR SHOW, THE AMAZING WORLD OF GUMBALL, ADVENTURE TIME]\n\n\/\/A\u5f00\u5934\u7684\u8f6c\u5927\u5199\ndef cartoonStartsAToUppercase = cartoons.collect { cartoon -&gt; self.uc(cartoon) }\nprintln cartoonStartsAToUppercase \/\/ [Regular Show, The Amazing World of Gumball, ADVENTURE TIME]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>list\u5217\u8868\u5b9a\u4e49 def name = &#039;John&#039; def surname = &#038;#03 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[568],"tags":[],"class_list":["post-2227","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2227"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2227\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}