{"id":34466,"date":"2024-11-26T15:18:43","date_gmt":"2024-11-26T07:18:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34466\/"},"modified":"2024-11-26T15:18:43","modified_gmt":"2024-11-26T07:18:43","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e8%a1%a8%e7%a4%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34466\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u8868\u793a\u5b57\u7b26\u4e32\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  java\u4e2d\u8868\u793a\u5b57\u7b26\u4e32\u6570\u7ec4\u6709\u56db\u79cd\u65b9\u6cd5\uff1a\u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4\u3001\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u3001\u4ece\u5176\u4ed6\u6570\u7ec4\u590d\u5236\u548c\u4f7f\u7528array.aslist()\u3002\u6570\u7ec4\u5143\u7d20\u53ef\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/02\/2024110214302022634.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u8868\u793a\u5b57\u7b26\u4e32\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u8868\u793a\u5b57\u7b26\u4e32\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u8868\u793a\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8868\u793a\u5b57\u7b26\u4e32\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4<\/strong><\/p>\n<pre>String[] names = new String[]{\"John\", \"Mary\", \"Bob\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u8fd9\u4e00\u884c\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u540d\u4e3anames\u7684\u65b0\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528\u521d\u59cb\u5316\u5668\u5217\u8868\u5c06\u5176\u521d\u59cb\u5316\u4e09\u4e2a\u5143\u7d20\uff1aJohn\u3001Mary\u548cBob\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] names = {\"John\", \"Mary\", \"Bob\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u8fd9\u4e00\u884c\u4ee3\u7801\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u6765\u521b\u5efa\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u6570\u7ec4\u5b57\u9762\u91cf\u662f\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u5217\u8868\u7684\u82b1\u62ec\u53f7\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n<p><strong>3. \u4ece\u5176\u4ed6\u6570\u7ec4\u590d\u5236<\/strong><\/p>\n<pre>String[] originalNames = {\"John\", \"Mary\", \"Bob\"};\nString[] copyOfNames = Arrays.copyOf(originalNames, originalNames.length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u6570\u7ec4copyOfNames\uff0c\u5b83\u662foriginalNames\u6570\u7ec4\u7684\u526f\u672c\u3002Arrays.copyOf()\u65b9\u6cd5\u5c06\u6307\u5b9a\u6570\u7ec4\u7684\u5b50\u6570\u7ec4\u590d\u5236\u5230\u4e00\u4e2a\u65b0\u6570\u7ec4\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>4. \u4f7f\u7528Array.asList()<\/strong><\/p>\n<pre>List&lt;String&gt; namesList = Arrays.asList(\"John\", \"Mary\", \"Bob\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528Arrays.asList()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u4f46\u662f\u8981\u6ce8\u610f\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u521b\u5efa\u7684\u6570\u7ec4\u5728\u6280\u672f\u4e0a\u5e76\u4e0d\u662f\u4e00\u4e2a\u771f\u6b63\u7684\u6570\u7ec4\uff0c\u800c\u662f\u4e00\u4e2aList\u7684\u89c6\u56fe\u3002\u8fd9\u610f\u5473\u7740\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><strong>\u8bbf\u95ee\u548c\u66f4\u6539\u6570\u7ec4\u5143\u7d20<\/strong><\/p>\n<p>\u8981\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>String name = names[0];  \/\/ \u83b7\u53d6\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8981\u66f4\u6539\u6570\u7ec4\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>names[0] = \"Jane\";  \/\/ \u5c06\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u66f4\u6539\u4e3a \"Jane\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u8868\u793a\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\u8868\u793a\u5b57\u7b26\u4e32\u6570\u7ec4\u6709\u56db\u79cd\u65b9\u6cd5\uff1a\u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4\u3001\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u3001\u4ece\u5176\u4ed6\u6570\u7ec4\u590d\u5236\u548c\u4f7f\u7528array.aslist()\u3002\u6570\u7ec4\u5143\u7d20\u53ef\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u3002 Java\u4e2d\u8868\u793a\u7684\u65b9\u6cd5 \u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8868\u793a\u5b57\u7b26\u4e32\u6570\u7ec4\uff1a 1. \u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4 String[] names = new String[]{&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;}; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e00\u884c\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u540d\u4e3anames\u7684\u65b0\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528\u521d\u59cb\u5316\u5668\u5217\u8868\u5c06\u5176\u521d\u59cb\u5316\u4e09\u4e2a\u5143\u7d20\uff1aJohn\u3001Mary\u548cBob\u3002 2. \u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] names = {&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;}; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e00\u884c\u4ee3\u7801\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u6765\u521b\u5efa\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u6570\u7ec4\u5b57\u9762\u91cf\u662f\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u5217\u8868\u7684\u82b1\u62ec\u53f7\u8868\u8fbe\u5f0f\u3002 3. \u4ece\u5176\u4ed6\u6570\u7ec4\u590d\u5236 String[] originalNames = {&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;}; String[] copyOfNames = Arrays.copyOf(originalNames, originalNames.length); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u6570\u7ec4copyOfNames\uff0c\u5b83\u662foriginalNames\u6570\u7ec4\u7684\u526f\u672c\u3002Arrays.copyOf()\u65b9\u6cd5\u5c06\u6307\u5b9a\u6570\u7ec4\u7684\u5b50\u6570\u7ec4\u590d\u5236\u5230\u4e00\u4e2a\u65b0\u6570\u7ec4\u4e2d\u3002 4. \u4f7f\u7528Array.asList() List&lt;String&gt; namesList = Arrays.asList(&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528Arrays.asList()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u4f46\u662f\u8981\u6ce8\u610f\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u521b\u5efa\u7684\u6570\u7ec4\u5728\u6280\u672f\u4e0a\u5e76\u4e0d\u662f\u4e00\u4e2a\u771f\u6b63\u7684\u6570\u7ec4\uff0c\u800c\u662f\u4e00\u4e2aList\u7684\u89c6\u56fe\u3002\u8fd9\u610f\u5473\u7740\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002 \u8bbf\u95ee\u548c\u66f4\u6539\u6570\u7ec4\u5143\u7d20 \u8981\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a String name = [&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-34466","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34466","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=34466"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34466\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}