{"id":37774,"date":"2024-11-26T14:00:36","date_gmt":"2024-11-26T06:00:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37774\/"},"modified":"2024-11-26T14:00:36","modified_gmt":"2024-11-26T06:00:36","slug":"java%e4%b8%ad%e6%95%b0%e7%bb%84%e8%b6%8a%e7%95%8c%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37774\/","title":{"rendered":"java\u4e2d\u6570\u7ec4\u8d8a\u754c\u600e\u4e48\u89e3\u51b3"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u6570\u7ec4\u8d8a\u754c\u7684\u89e3\u51b3\u65b9\u6cd5\u6709\uff1a\u8303\u56f4\u68c0\u67e5\u4f7f\u7528 system.arraycopy()\u4f7f\u7528 arrays.copyof()\u4f7f\u7528\u5faa\u73af\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111310214686374.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u6570\u7ec4\u8d8a\u754c\u600e\u4e48\u89e3\u51b3\u63d2\u56fe\" alt=\"java\u4e2d\u6570\u7ec4\u8d8a\u754c\u600e\u4e48\u89e3\u51b3\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u6570\u7ec4\u8d8a\u754c\u7684\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u6570\u7ec4\u8d8a\u754c\uff1f<\/strong><\/p>\n<p>\u6570\u7ec4\u8d8a\u754c\u662f\u6307\u8bbf\u95ee\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u3002\u5f53\u6570\u7ec4\u7d22\u5f15\u8d85\u51fa\u6570\u7ec4\u5927\u5c0f\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u6570\u7ec4\u8d8a\u754c\u3002<\/p>\n<p><strong>\u89e3\u51b3\u6570\u7ec4\u8d8a\u754c\u7684\u5e38\u89c1\u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>1. \u8303\u56f4\u68c0\u67e5<\/strong><\/p>\n<p>\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u4e4b\u524d\uff0c\u663e\u5f0f\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5728\u6570\u7ec4\u8fb9\u754c\u5185\u3002\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u8fb9\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u6216\u6267\u884c\u5176\u4ed6\u9519\u8bef\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<pre>int[] arr = new int[5];\n\ntry {\n    System.out.println(arr[5]); \/\/ \u4f1a\u629b\u51fa ArrayIndexOutOfBoundsException\n} catch (ArrayIndexOutOfBoundsException e) {\n    System.out.println(\"\u6570\u7ec4\u8d8a\u754c\uff1a\" + e.getMessage());\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 System.arraycopy()<\/strong><\/p>\n<p>System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u590d\u5236\u6570\u7ec4\u5143\u7d20\uff0c\u907f\u514d\u6570\u7ec4\u8d8a\u754c\u3002\u8be5\u65b9\u6cd5\u6307\u5b9a\u6e90\u6570\u7ec4\u3001\u76ee\u6807\u6570\u7ec4\u3001\u5f00\u59cb\u590d\u5236\u7684\u7d22\u5f15\u548c\u8981\u590d\u5236\u7684\u5143\u7d20\u6570\u91cf\u3002<\/p>\n<pre>int[] src = {1, 2, 3, 4, 5};\nint[] dest = new int[src.length];\n\nSystem.arraycopy(src, 0, dest, 0, src.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.copyOf()<\/strong><\/p>\n<p>Arrays.copyOf() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u662f\u539f\u59cb\u6570\u7ec4\u7684\u4e00\u4e2a\u526f\u672c\uff0c\u5927\u5c0f\u53ef\u4ee5\u8c03\u6574\u5230\u6307\u5b9a\u7684\u957f\u5ea6\u3002\u5982\u679c\u539f\u59cb\u6570\u7ec4\u6bd4\u6307\u5b9a\u957f\u5ea6\u77ed\uff0c\u5219\u65b0\u6570\u7ec4\u5c06\u586b\u5145\u9ed8\u8ba4\u503c\u3002\u5982\u679c\u539f\u59cb\u6570\u7ec4\u6bd4\u6307\u5b9a\u957f\u5ea6\u957f\uff0c\u5219\u65b0\u6570\u7ec4\u5c06\u622a\u65ad\u591a\u4f59\u7684\u5143\u7d20\u3002<\/p>\n<pre>int[] src = {1, 2, 3, 4, 5};\n\nint[] dest1 = Arrays.copyOf(src, src.length); \/\/ \u4e0e\u539f\u59cb\u6570\u7ec4\u76f8\u540c\u5927\u5c0f\u7684\u526f\u672c\nint[] dest2 = Arrays.copyOf(src, 3); \/\/ \u622a\u65ad\u540e\u4e3a 3 \u4e2a\u5143\u7d20\u7684\u526f\u672c<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u5faa\u73af<\/strong><\/p>\n<p>\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u660e\u786e\u5730\u904d\u5386\u6570\u7ec4\uff0c\u907f\u514d\u6570\u7ec4\u8d8a\u754c\u3002<\/p>\n<pre>int[] arr = {1, 2, 3, 4, 5};\n\nfor (int i = 0; i &lt; arr.length; i++) {\n    \/\/ \u5b89\u5168\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\n    System.out.println(arr[i]);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u6570\u7ec4\u8d8a\u754c\u600e\u4e48\u89e3\u51b3\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\u6570\u7ec4\u8d8a\u754c\u7684\u89e3\u51b3\u65b9\u6cd5\u6709\uff1a\u8303\u56f4\u68c0\u67e5\u4f7f\u7528 system.arraycopy()\u4f7f\u7528 arrays.copyof()\u4f7f\u7528\u5faa\u73af Java \u4e2d\u6570\u7ec4\u8d8a\u754c\u7684\u89e3\u51b3\u65b9\u6cd5 \u4ec0\u4e48\u662f\u6570\u7ec4\u8d8a\u754c\uff1f \u6570\u7ec4\u8d8a\u754c\u662f\u6307\u8bbf\u95ee\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u3002\u5f53\u6570\u7ec4\u7d22\u5f15\u8d85\u51fa\u6570\u7ec4\u5927\u5c0f\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u6570\u7ec4\u8d8a\u754c\u3002 \u89e3\u51b3\u6570\u7ec4\u8d8a\u754c\u7684\u5e38\u89c1\u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1. \u8303\u56f4\u68c0\u67e5 \u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u4e4b\u524d\uff0c\u663e\u5f0f\u68c0\u67e5\u7d22\u5f15\u662f\u5426\u5728\u6570\u7ec4\u8fb9\u754c\u5185\u3002\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u8fb9\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u6216\u6267\u884c\u5176\u4ed6\u9519\u8bef\u5904\u7406\u64cd\u4f5c\u3002 int[] arr = new int[5]; try { System.out.println(arr[5]); \/\/ \u4f1a\u629b\u51fa ArrayIndexOutOfBoundsException } catch (ArrayIndexOutOfBoundsException e) { System.out.println(&#8220;\u6570\u7ec4\u8d8a\u754c\uff1a&#8221; + e.getMessage()); } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 System.arraycopy() System.arraycopy() \u65b9\u6cd5\u53ef\u4ee5\u5b89\u5168\u5730\u590d\u5236\u6570\u7ec4\u5143\u7d20\uff0c\u907f\u514d\u6570\u7ec4\u8d8a\u754c\u3002\u8be5\u65b9\u6cd5\u6307\u5b9a\u6e90\u6570\u7ec4\u3001\u76ee\u6807\u6570\u7ec4\u3001\u5f00\u59cb\u590d\u5236\u7684\u7d22\u5f15\u548c\u8981\u590d\u5236\u7684\u5143\u7d20\u6570\u91cf\u3002 int[] src = {1, 2, 3, 4, 5}; int[] dest = new int[src.length]; System.arraycopy(src, 0, dest, [&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-37774","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37774","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=37774"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37774\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}