{"id":38692,"date":"2024-11-26T11:39:37","date_gmt":"2024-11-26T03:39:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38692\/"},"modified":"2024-11-26T11:39:37","modified_gmt":"2024-11-26T03:39:37","slug":"java%e4%ba%a4%e6%8d%a2%e6%95%b0%e7%bb%84%e5%85%83%e7%b4%a0%e6%80%8e%e4%b9%88%e6%95%88%e7%8e%87%e9%ab%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38692\/","title":{"rendered":"java\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\u600e\u4e48\u6548\u7387\u9ad8"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u9ad8\u6548\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a1. \u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\uff1b2. \u4f7f\u7528\u4f4d\u8fd0\u7b97\uff1b3. \u4f7f\u7528\u4e09\u5143\u64cd\u4f5c\u7b26\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111505002254237.jpg\" class=\"aligncenter\" title=\"java\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\u600e\u4e48\u6548\u7387\u9ad8\u63d2\u56fe\" alt=\"java\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\u600e\u4e48\u6548\u7387\u9ad8\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u9ad8\u6548\u4ea4\u6362\u6570\u7ec4\u5143\u7d20<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u63d0\u5347\u6570\u7ec4\u5143\u7d20\u4ea4\u6362\u6548\u7387\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 System.arraycopy() \u65b9\u6cd5<\/strong><\/p>\n<p>System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u590d\u5236\u6570\u7ec4\u5143\u7d20\uff0c\u5305\u62ec\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\u3002\u5176\u6548\u7387\u9ad8\u4e8e\u9010\u4e2a\u5143\u7d20\u8d4b\u503c\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint temp = arr[0];\nSystem.arraycopy(arr, 1, arr, 0, arr.length - 1);  \/\/ \u5c06\u5143\u7d20 2 ~ 5 \u590d\u5236\u5230\u5143\u7d20 1 ~ 4\narr[arr.length - 1] = temp;  \/\/ \u5c06\u5143\u7d20 1 \u8d4b\u503c\u7ed9\u5143\u7d20 5<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u4f4d\u8fd0\u7b97<\/strong><\/p>\n<p>\u5bf9\u4e8e\u7c7b\u578b\u4e3a int \u6216 long \u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u6309\u4f4d\u5f02\u6216\u8fd0\u7b97 (^) \u6765\u9ad8\u6548\u4ea4\u6362\u5143\u7d20\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\narr[0] ^= arr[4];\narr[4] ^= arr[0];\narr[0] ^= arr[4];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u4e09\u5143\u64cd\u4f5c\u7b26<\/strong><\/p>\n<p>\u4e09\u5143\u64cd\u4f5c\u7b26\u4e5f\u53ef\u4ee5\u7528\u6765\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\uff0c\u4f46\u6548\u7387\u7565\u4f4e\u4e8e\u524d\u4e24\u79cd\u65b9\u6cd5\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\nint temp = arr[0];\narr[0] = (arr[4] == arr[0]) ? arr[4] : (arr[4] = arr[0]);\narr[4] = temp;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\u600e\u4e48\u6548\u7387\u9ad8\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>\u5728 java \u4e2d\uff0c\u9ad8\u6548\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a1. \u4f7f\u7528 system.arraycopy() \u65b9\u6cd5\uff1b2. \u4f7f\u7528\u4f4d\u8fd0\u7b97\uff1b3. \u4f7f\u7528\u4e09\u5143\u64cd\u4f5c\u7b26\u3002 Java \u4e2d\u9ad8\u6548\u4ea4\u6362\u6570\u7ec4\u5143\u7d20 \u5728 Java \u4e2d\uff0c\u63d0\u5347\u6570\u7ec4\u5143\u7d20\u4ea4\u6362\u6548\u7387\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 System.arraycopy() \u65b9\u6cd5 System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u590d\u5236\u6570\u7ec4\u5143\u7d20\uff0c\u5305\u62ec\u4ea4\u6362\u4e24\u4e2a\u5143\u7d20\u3002\u5176\u6548\u7387\u9ad8\u4e8e\u9010\u4e2a\u5143\u7d20\u8d4b\u503c\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3, 4, 5}; int temp = arr[0]; System.arraycopy(arr, 1, arr, 0, arr.length &#8211; 1); \/\/ \u5c06\u5143\u7d20 2 ~ 5 \u590d\u5236\u5230\u5143\u7d20 1 ~ 4 arr[arr.length &#8211; 1] = temp; \/\/ \u5c06\u5143\u7d20 [&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-38692","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38692","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=38692"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38692\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}