{"id":38793,"date":"2024-11-26T11:59:25","date_gmt":"2024-11-26T03:59:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38793\/"},"modified":"2024-11-26T11:59:25","modified_gmt":"2024-11-26T03:59:25","slug":"java-%e6%80%8e%e4%b9%88%e6%89%93%e4%b9%b1%e6%95%b0%e7%bb%84%e9%a1%ba%e5%ba%8f%e6%8e%92%e5%88%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38793\/","title":{"rendered":"java \u600e\u4e48\u6253\u4e71\u6570\u7ec4\u987a\u5e8f\u6392\u5217"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6253\u4e71 java \u6570\u7ec4\u7684\u987a\u5e8f\uff1a\u4f7f\u7528 collections.shuffle \u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528 fisher-yates \u6d17\u724c\u7b97\u6cd5\u3002\u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668\u751f\u6210\u968f\u673a\u6570\u6765\u91cd\u65b0\u6392\u5217\u5143\u7d20\u3002\u4f7f\u7528\u5faa\u73af\u548c\u4ea4\u6362\u624b\u52a8\u91cd\u65b0\u6392\u5217\u5143\u7d20\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111506395041529.jpg\" class=\"aligncenter\" title=\"java \u600e\u4e48\u6253\u4e71\u6570\u7ec4\u987a\u5e8f\u6392\u5217\u63d2\u56fe\" alt=\"java \u600e\u4e48\u6253\u4e71\u6570\u7ec4\u987a\u5e8f\u6392\u5217\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u6253\u4e71 Java \u6570\u7ec4\u7684\u987a\u5e8f<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6253\u4e71\u987a\u5e8f\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Collections.shuffle<\/strong><\/p>\n<p>Collections.shuffle \u662f Java Collections \u6846\u67b6\u4e2d\u5185\u7f6e\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u6253\u4e71\u6570\u7ec4\u7684\u987a\u5e8f\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>Collections.shuffle(list);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d list \u662f\u9700\u8981\u6253\u4e71\u987a\u5e8f\u7684\u6570\u7ec4\u3002Collections.shuffle \u4f7f\u7528 Fisher-Yates \u6d17\u724c\u7b97\u6cd5\uff0c\u5b83\u80fd\u4fdd\u8bc1\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4ee5\u5747\u5300\u7684\u6982\u7387\u88ab\u91cd\u65b0\u3002<\/p>\n<p><strong>2. \u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 Java \u7684 Random \u7c7b\u6765\u751f\u6210\u968f\u673a\u6570\uff0c\u5e76\u6839\u636e\u8fd9\u4e9b\u968f\u673a\u6570\u6765\u6253\u4e71\u6570\u7ec4\u7684\u987a\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre>Random random = new Random();\nfor (int i = 0; i &lt; array.length; i++) {\n    int randomIndex = random.nextInt(array.length);\n    int temp = array[i];\n    array[i] = array[randomIndex];\n    array[randomIndex] = temp;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u7f3a\u70b9\u662f\uff0c\u5b83\u53ef\u80fd\u4ea7\u751f\u91cd\u590d\u7684\u968f\u673a\u7d22\u5f15\uff0c\u4ece\u800c\u5bfc\u81f4\u6570\u7ec4\u4e2d\u7684\u67d0\u4e9b\u5143\u7d20\u4e0d\u4f1a\u88ab\u91cd\u65b0\u6392\u5217\u3002<\/p>\n<p><strong>3. \u4f7f\u7528\u5faa\u73af\u548c\u4ea4\u6362<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u4ea4\u6362\u6765\u624b\u52a8\u6253\u4e71\u6570\u7ec4\u7684\u987a\u5e8f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1a<\/p>\n<pre>for (int i = 0; i &lt; array.length; i++) {\n    int randomIndex = (int) (Math.random() * array.length);\n    int temp = array[i];\n    array[i] = array[randomIndex];\n    array[randomIndex] = temp;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u6709\u70b9\u5197\u957f\uff0c\u4f46\u5b83\u786e\u4fdd\u4e86\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4e0d\u4f1a\u88ab\u91cd\u590d\u9009\u62e9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u600e\u4e48\u6253\u4e71\u6570\u7ec4\u987a\u5e8f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6253\u4e71 java \u6570\u7ec4\u7684\u987a\u5e8f\uff1a\u4f7f\u7528 collections.shuffle \u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528 fisher-yates \u6d17\u724c\u7b97\u6cd5\u3002\u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668\u751f\u6210\u968f\u673a\u6570\u6765\u91cd\u65b0\u6392\u5217\u5143\u7d20\u3002\u4f7f\u7528\u5faa\u73af\u548c\u4ea4\u6362\u624b\u52a8\u91cd\u65b0\u6392\u5217\u5143\u7d20\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002 \u5982\u4f55\u6253\u4e71 Java \u6570\u7ec4\u7684\u987a\u5e8f \u5728 Java \u4e2d\uff0c\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6253\u4e71\u987a\u5e8f\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a 1. \u4f7f\u7528 Collections.shuffle Collections.shuffle \u662f Java Collections \u6846\u67b6\u4e2d\u5185\u7f6e\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u6253\u4e71\u6570\u7ec4\u7684\u987a\u5e8f\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Collections.shuffle(list); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d list \u662f\u9700\u8981\u6253\u4e71\u987a\u5e8f\u7684\u6570\u7ec4\u3002Collections.shuffle \u4f7f\u7528 Fisher-Yates \u6d17\u724c\u7b97\u6cd5\uff0c\u5b83\u80fd\u4fdd\u8bc1\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4ee5\u5747\u5300\u7684\u6982\u7387\u88ab\u91cd\u65b0\u3002 2. \u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668 \u53ef\u4ee5\u4f7f\u7528 Java \u7684 Random \u7c7b\u6765\u751f\u6210\u968f\u673a\u6570\uff0c\u5e76\u6839\u636e\u8fd9\u4e9b\u968f\u673a\u6570\u6765\u6253\u4e71\u6570\u7ec4\u7684\u987a\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\uff1a Random random = new Random(); for (int i = 0; i &lt; array.length; i++) { int randomIndex = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-38793","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38793"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38793\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}