{"id":2231,"date":"2023-05-06T22:05:47","date_gmt":"2023-05-06T14:05:47","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2231"},"modified":"2023-05-06T22:05:53","modified_gmt":"2023-05-06T14:05:53","slug":"groovy-code-example-usage-of-array","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/06\/groovy-code-example-usage-of-array\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; \u6570\u7ec4\u7684\u4f7f\u7528"},"content":{"rendered":"<p>\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\uff0c\u5df2\u7ecf\u4ecb\u7ecd\u4e86groovy\u7684list\u4f7f\u7528\u3002\u672c\u7ae0\u5c06\u4ecb\u7ecdgroovy\u7684\u6570\u7ec4\u3002\u867d\u7136\u6570\u7ec4\u548c\u5217\u8868\u770b\u4e0a\u53bb\u5f88\u76f8\u4f3c\uff0c\u4f46\u662f\u8fd8\u662f\u6709\u4e00\u4e9b\u6839\u672c\u7684\u4e0d\u540c\u3002\u4f8b\u5982\uff0c\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\u800clist\u7684\u5927\u5c0f\u662f\u52a8\u6001\u7684\u3002\u8fd9\u610f\u5473\u7740\u5217\u8868\u4e2d\u53ef\u4ee5\u5b58\u653e\u4efb\u610f\u591a\u4e2a\u5bf9\u8c61\uff0c\u800c\u5bf9\u4e8e\u6570\u7ec4\uff0c\u5982\u679c\u4f60\u5b58\u7684\u5bf9\u8c61\u4e2a\u6570\u591a\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5c06\u4f1a\u629b\u51fa\uff1a<code>MissingMethodException<\/code>\u6216<code>ArrayIndexOutOfBoundsException<\/code>\u3002\u6211\u4eec\u770b\u4e00\u4e0bgroovy\u6570\u7ec4\u5177\u4f53\u7684\u5e94\u7528\u3002<\/p>\n<p>1\u3001\u6570\u7ec4\u5b9a\u4e49\uff1a<\/p>\n<pre><code class=\"language-java\">def birds = new String[3]\nbirds[0] = &quot;Parrot&quot;\nbirds.putAt(1, &quot;Cockatiel&quot;)\nbirds[2] = &quot;Pigeon&quot;\n\nprintln birds \/\/ [Parrot, Cockatiel, Pigeon]\n\ndef birdArr = [&quot;Parrot&quot;, &quot;Cockatiel&quot;, &quot;Pigeon&quot;] as String[] \/\/\u663e\u5f0f\u58f0\u660e\u4e3a\u6570\u7ec4\nprintln birdArr \/\/ [Parrot, Cockatiel, Pigeon]<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u4f60\u53ef\u4ee5\u58f0\u660e\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u8fd8\u53ef\u4ee5\u50cfjava\u4e2d\u90a3\u6837\u4e3a\u6570\u7ec4\u8d4b\u503c\u3002<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u76f4\u63a5\u4f7f\u7528\u521d\u59cb\u503c\u58f0\u660e\u6570\u7ec4\u5e76\u663e\u5f0f\u7684\u58f0\u660e\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u3002<\/p>\n<p>2\u3001\u8bbf\u95ee\u6570\u7ec4\u5bf9\u8c61<\/p>\n<p>\u5728groovy\u4e2d\uff0c\u4f60\u53ef\u4ee5\u50cfjava\u4e2d\u90a3\u6837\u8bbf\u95ee\u6570\u7ec4<code>arr[index]<\/code>\u6216\u662f<code>getAt(index)<\/code>\u65b9\u5f0f\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-java\">def birds = [&quot;Parrot&quot;, &quot;Cockatiel&quot;, &quot;Pigeon&quot;] as String[]\n\nprintln birds[0] \/\/ Parrot\nprintln birds[2] \/\/ Pigeon\n\nprintln birds.getAt(1) \/\/ Cockatiel\n\nprintln birds[-1] \/\/ Pigeon\nprintln birds[-3] \/\/ Parrot<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u6570\u7ec4\u662f\u53ef\u4ee5\u652f\u6301\u7d22\u5f15\u4e3a\u8d1f\u6570\uff0c\u5f53\u7d22\u5f15\u53f7\u4e3a\u8d1f\u6570\u65f6\uff0c\u6570\u7ec4\u662f\u4ece\u5c3e\u90e8\u5f00\u59cb\u8ba1\u6570\u7684\uff0c\u4ece<code>-1<\/code>\u5f00\u59cb\u8ba1\u6570\uff0c<code>-1<\/code>\u8868\u793a\u6570\u7ec4\u4e2d\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u540c\u7406<code>-3<\/code>\u8868\u793a\u5012\u6570\u7b2c\u4e09\u4e2a\u5143\u7d20\u3002<\/p>\n<p>3\u3001\u6570\u7ec4\u8bbf\u95ee\u5f02\u5e38<\/p>\n<p>\u6b63\u5982\u524d\u9762\u6240\u8bf4\uff0c\u5982\u679c\u4f60\u8bd5\u56fe\u5f80\u6570\u7ec4\u4e2d\u589e\u52a0\u5143\u7d20\u5e76\u8d85\u8fc7\u6570\u7ec4\u957f\u5ea6\u7684\u8bdd\uff0c\u5c06\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"language-java\">def birds = [&quot;Parrot&quot;, &quot;Cockatiel&quot;, &quot;Pigeon&quot;] as String[]\n\nbirds &lt;&lt; &quot;Eagle&quot; \/\/ MissingMethodException\n\nbirds.putAt(3, &quot;Owl&quot;) \/\/ ArrayIndexOutOfBoundsException<\/code><\/pre>\n<p><code>&lt;&lt;<\/code>\u662f\u5217\u8868list\u7684\u65b9\u6cd5\uff0c\u5f53\u6211\u4eec\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528<code>&lt;&lt;<\/code>\u65b9\u6cd5\u65f6\uff0c\u5c06\u4f1a\u629b\u51fa<code>MissingMethodException<\/code>\u3002\u5982\u679c\u8bbf\u95ee\u6570\u7ec4\u5bf9\u8c61\u8d85\u8fc7\u6570\u7ec4\u7684\u5927\u5c0f\u65f6\u4f1a\u629b\u51fa<code>ArrayIndexOutOfBounddException<\/code>\u3002<\/p>\n<p>4\u3001\u6570\u7ec4\u957f\u5ea6<\/p>\n<p>Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>size()<\/code>\u65b9\u6cd5\u83b7\u53d6list\u7684\u957f\u5ea6\uff0c\u4f7f\u7528length\u5c5e\u6027\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u5728groovy\u4e2d\uff0c\u4f60\u65e2\u53ef\u4ee5\u4f7f\u7528<code>size()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u4f7f\u7528length\u5c5e\u6027\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<pre><code class=\"language-java\">def birds = [&quot;Parrot&quot;, &quot;Cockatiel&quot;, &quot;Pigeon&quot;] as String[]\n\nprintln birds.length \/\/ 3\n\nprintln birds.size() \/\/ 3<\/code><\/pre>\n<p>5\u3001\u6570\u7ec4\u6700\u5927\u3001\u6700\u5c0f\u503c<\/p>\n<p>\u6bd4\u5982\u8bf4\u5427\uff0c\u4f60\u6709\u4e00\u4e2a\u6570\u5b57\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u4e0a\u7684min()\u548cmax()\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6700\u5c0f\u3001\u6700\u5927\u503c\u3002<\/p>\n<pre><code class=\"language-java\">def numbers = [32, 44, 12, 9, 100, 180]\nprintln numbers.max() \/\/ 180\nprintln numbers.min() \/\/ 9\n\ndef birds = [&quot;Parrot&quot;, &quot;Cockatiel&quot;, &quot;Pigeon&quot;] as String[]\nprintln birds.max { it.size() } \/\/ Cockatiel\nprintln birds.min { it.size() } \/\/ Parrot<\/code><\/pre>\n<p>\u524d\u9762\u4e00\u6bb5\u5f88\u5bb9\u6613\u7406\u89e3\u3002\u5bf9\u4e8e\u540e\u9762\u7684\u4e00\u6bb5\uff0cmax\u548cmin\u65b9\u6cd5\u5176\u5b9e\u4f5c\u7528\u4e8e\u6570\u7ec4\u4e2d\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u8fd9\u91cc\u4f7f\u7528\u4e86\u95ed\u5305\u7684\u65b9\u5f0f\uff0cit\u4ee3\u8868\u7684\u662f\u6570\u7ec4\u4e2d\u7684\u5bf9\u8c61\u3002<\/p>\n<p>6\u3001\u5220\u9664\u6570\u7ec4\u4e2d\u5143\u7d20<\/p>\n<p>groovy\u4e2d\u5220\u9664\u6570\u7ec4\u5143\u7d20\u4e0d\u662f\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u5220\u9664\uff0c\u5176\u5b9e\u662f\u4e3a\u6570\u7ec4\u91cd\u65b0\u8d4b\u503c\u53e6\u5916\u4e00\u4e2a\u5df2\u7ecf\u5220\u9664\u5143\u7d20\u7684\u6570\u7ec4\u3002<\/p>\n<pre><code class=\"language-java\">def birds = [&quot;Parrot&quot;, &quot;Cockatiel&quot;, &quot;Pigeon&quot;] as String[]\n\ndef birdsWithoutParrot = birds - &quot;Parrot&quot;\n\nprintln birds \/\/ [Parrot, Cockatiel, Pigeon]\nprintln birdsWithoutParrot \/\/ [Cockatiel, Pigeon]<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u201c\u51cf\u6cd5\u201d\u64cd\u4f5c\u4ece\u6570\u7ec4\u4e2d\u79fb\u9664\u4e00\u4e2a\u5143\u7d20\u5e76\u8d4b\u503c\u7ed9\u65b0\u7684\u6570\u7ec4\u3002\u4f46\u662f\uff0c\u8001\u7684\u6570\u7ec4\u5176\u5b9e\u5e76\u6ca1\u6709\u53d8\u5316\u3002<\/p>\n<p>7\u3001\u6570\u7ec4\u6392\u5e8f<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u96c6\u5408\uff0c\u53ef\u80fd\u7ecf\u5e38\u9700\u8981\u6392\u5e8f\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u4ee5\u5bf9\u6570\u7ec4\u53cd\u5411\u6392\u5e8f\u6216\u662f\u6309\u7167\u6307\u5b9a\u89c4\u5219\u6392\u5e8f\u3002<\/p>\n<pre><code class=\"language-java\">def birds = [&quot;Parrot&quot;, &quot;Cockatiel&quot;, &quot;Pigeon&quot;] as String[]\nprintln birds.reverse() \/\/ [Pigeon, Cockatiel, Parrot]\nprintln birds.sort() \/\/ [Cockatiel, Parrot, Pigeon]<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u5206\u522b\u5bf9\u6570\u7ec4\u91c7\u7528\u4e86\u53cd\u5e8f\u4ee5\u53ca\u6309\u7167\u5b57\u6bcd\u987a\u5e8f\u6392\u5e8f\u3002<\/p>\n<p>8\u3001\u6570\u7ec4\u67e5\u627e<\/p>\n<p>\u4f60\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6570\u636e\uff0c\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u628a\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u4e32\u53cd\u8f6c\uff0c\u5e76\u4e14\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u6570\u7ec4\u3002<\/p>\n<pre><code class=\"language-java\">def birds = [&quot;Parrot&quot;, &quot;Cockatiel&quot;, &quot;Pigeon&quot;] as String[]\n\ndef revertedBirds = birds.collect { it.reverse() }\nprintln revertedBirds \/\/ [torraP, leitakcoC, noegiP]\n\ndef founded = birds.find { it =~ \/Cockatiel\/ }\nprintln founded \/\/ Cockatiel<\/code><\/pre>\n<p>\u6211\u4eec\u4f7f\u7528collect\u65b9\u6cd5\u904d\u5386\u6570\u7ec4\u5e76\u53cd\u8f6c\u5b57\u7b26\u4e32\u5e76\u4f7f\u7528find\u65b9\u6cd5\u67e5\u627e\u6570\u7ec4\u5185\u5bb9\u3002<\/p>\n<p>9\u3001\u6570\u7ec4\u8f6c\u6362<\/p>\n<p>\u5728groovy\u4e2d\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-java\">def birds = [&quot;Parrot&quot;, &quot;Cockatiel&quot;, &quot;Pigeon&quot;] as String[]\ndef birdList = birds.toList()\nprintln birdList.class.name \/\/ java.util.ArrayList\n\ndef birdsAgain = birdList as String[]\nprintln birdsAgain.class.name \/\/ [Ljava.lang.String;<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u6211\u4eec\u901a\u8fc7toList\u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u6210\u4e3a\u5217\u8868\u5bf9\u8c61\uff0c\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5c06\u5217\u8868\u8f6c\u6362\u6210\u6570\u7ec4\u3002<\/p>\n<p>\u603b\u7684\u8bf4\u6765\uff0c\u6570\u7ec4\u548c\u5217\u8868\u662f\u5f88\u76f8\u4f3c\u7684\uff0c\u6700\u5927\u7684\u5dee\u522b\u5c31\u662f\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\u800c\u5217\u8868\u7684\u5927\u5c0f\u662f\u52a8\u6001\u6269\u5c55\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\uff0c\u5df2\u7ecf\u4ecb\u7ecd\u4e86groovy\u7684list\u4f7f\u7528\u3002\u672c\u7ae0\u5c06\u4ecb\u7ecdgroovy\u7684\u6570\u7ec4\u3002\u867d\u7136\u6570\u7ec4\u548c\u5217\u8868\u770b\u4e0a\u53bb\u5f88\u76f8\u4f3c [&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-2231","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2231","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=2231"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2231\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}