{"id":38034,"date":"2024-11-26T17:18:20","date_gmt":"2024-11-26T09:18:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38034\/"},"modified":"2024-11-26T17:18:20","modified_gmt":"2024-11-26T09:18:20","slug":"java%e4%b8%ad%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%85%91%e6%8d%a2%e4%bd%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38034\/","title":{"rendered":"java\u4e2d\u6570\u7ec4\u600e\u4e48\u5151\u6362\u4f4d\u7f6e"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\u4f4d\u7f6e\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a1. \u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u4ea4\u6362\uff1b2. \u4f7f\u7528\u4f4d\u5f02\u6216\u64cd\u4f5c\u7b26\u4ea4\u6362\uff1b3. \u4f7f\u7528 collections.swap() \u65b9\u6cd5\uff08\u4ec5\u9650 java 8 \u53ca\u66f4\u9ad8\u7248\u672c\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111315462337862.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u6570\u7ec4\u600e\u4e48\u5151\u6362\u4f4d\u7f6e\u63d2\u56fe\" alt=\"java\u4e2d\u6570\u7ec4\u600e\u4e48\u5151\u6362\u4f4d\u7f6e\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u6570\u7ec4\u4ea4\u6362\u5143\u7d20\u4f4d\u7f6e\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u4ea4\u6362\u6570\u7ec4\u4e2d\u4e24\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u4ea4\u6362<\/strong><\/p>\n<ul>\n<li>\u58f0\u660e\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u6765\u5b58\u50a8\u5176\u4e2d\u4e00\u4e2a\u5143\u7d20\u7684\u503c\u3002<\/li>\n<li>\u5c06\u53e6\u4e00\u4e2a\u5143\u7d20\u7684\u503c\u5b58\u50a8\u5230\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e2d\u3002<\/li>\n<li>\u5c06\u4e34\u65f6\u53d8\u91cf\u4e2d\u7684\u503c\u5b58\u50a8\u5230\u53e6\u4e00\u4e2a\u5143\u7d20\u4e2d\u3002<\/li>\n<\/ul>\n<pre>int[] arr = {1, 2, 3, 4, 5};\n\nint temp = arr[2];\narr[2] = arr[3];\narr[3] = temp;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u4f4d\u5f02\u6216\u64cd\u4f5c\u7b26\u4ea4\u6362<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u4f7f\u7528\u5f02\u6216\u64cd\u4f5c\u7b26 (^) \u5bf9\u4e24\u4e2a\u5143\u7d20\u8fdb\u884c\u5f02\u6216\u64cd\u4f5c\uff0c\u5c06\u8fd9\u4e24\u4e2a\u503c\u4ea4\u6362\u3002<\/li>\n<li>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u9700\u8981\u4e34\u65f6\u53d8\u91cf\uff0c\u4f46\u9700\u8981\u591a\u6b21\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<pre>int[] arr = {1, 2, 3, 4, 5};\n\narr[2] = arr[2] ^ arr[3];\narr[3] = arr[2] ^ arr[3];\narr[2] = arr[2] ^ arr[3];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Collections.swap() \u65b9\u6cd5\uff08\u4ec5\u9650 Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\uff09<\/strong><\/p>\n<ul>\n<li>Collections.swap() \u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u8bed\u6cd5\u6765\u4ea4\u6362\u5217\u8868\u6216\u6570\u7ec4\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\u3002<\/li>\n<li>\u8fd9\u4e2a\u65b9\u6cd5\u662f Collections \u7c7b\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u9700\u8981\u4f20\u9012\u6570\u7ec4\u548c\u5143\u7d20\u7d22\u5f15\u4f5c\u4e3a\u53c2\u6570\u3002<\/li>\n<\/ul>\n<pre>int[] arr = {1, 2, 3, 4, 5};\n\nCollections.swap(arr, 2, 3);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u4ea4\u6362\u6570\u7ec4\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\u3002\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4e2a\u4eba\u7684\u559c\u597d\u548c\u4ee3\u7801\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u6570\u7ec4\u600e\u4e48\u5151\u6362\u4f4d\u7f6e\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>java \u4e2d\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\u4f4d\u7f6e\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a1. \u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u4ea4\u6362\uff1b2. \u4f7f\u7528\u4f4d\u5f02\u6216\u64cd\u4f5c\u7b26\u4ea4\u6362\uff1b3. \u4f7f\u7528 collections.swap() \u65b9\u6cd5\uff08\u4ec5\u9650 java 8 \u53ca\u66f4\u9ad8\u7248\u672c\uff09\u3002 Java \u4e2d\u6570\u7ec4\u4ea4\u6362\u5143\u7d20\u4f4d\u7f6e\u7684\u65b9\u6cd5 \u5728 Java \u4e2d\uff0c\u4ea4\u6362\u6570\u7ec4\u4e2d\u4e24\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a 1. \u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u4ea4\u6362 \u58f0\u660e\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u6765\u5b58\u50a8\u5176\u4e2d\u4e00\u4e2a\u5143\u7d20\u7684\u503c\u3002 \u5c06\u53e6\u4e00\u4e2a\u5143\u7d20\u7684\u503c\u5b58\u50a8\u5230\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e2d\u3002 \u5c06\u4e34\u65f6\u53d8\u91cf\u4e2d\u7684\u503c\u5b58\u50a8\u5230\u53e6\u4e00\u4e2a\u5143\u7d20\u4e2d\u3002 int[] arr = {1, 2, 3, 4, 5}; int temp = arr[2]; arr[2] = arr[3]; arr[3] = temp; \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u4f4d\u5f02\u6216\u64cd\u4f5c\u7b26\u4ea4\u6362 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f7f\u7528\u5f02\u6216\u64cd\u4f5c\u7b26 (^) \u5bf9\u4e24\u4e2a\u5143\u7d20\u8fdb\u884c\u5f02\u6216\u64cd\u4f5c\uff0c\u5c06\u8fd9\u4e24\u4e2a\u503c\u4ea4\u6362\u3002 \u8fd9\u79cd\u65b9\u6cd5\u4e0d\u9700\u8981\u4e34\u65f6\u53d8\u91cf\uff0c\u4f46\u9700\u8981\u591a\u6b21\u64cd\u4f5c\u3002 int[] arr = {1, 2, 3, 4, 5}; arr[2] [&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-38034","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38034","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=38034"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38034\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}