{"id":38658,"date":"2024-11-26T12:05:30","date_gmt":"2024-11-26T04:05:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38658\/"},"modified":"2024-11-26T12:05:30","modified_gmt":"2024-11-26T04:05:30","slug":"java%e4%b8%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38658\/","title":{"rendered":"java\u4e2d\u5b57\u7b26\u4e32\u6570\u7ec4\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u7f16\u5199\u5b57\u7b26\u4e32\u6570\u7ec4\u9700\u8981\u521b\u5efa\u4e00\u4e2a string[] \u7c7b\u578b\u53d8\u91cf\u5e76\u5206\u914d\u503c\u3002\u60a8\u53ef\u4ee5\u91c7\u7528\u9010\u4e00\u8d4b\u503c\u3001\u6570\u7ec4\u521d\u59cb\u5316\u5217\u8868\u6216 arrays.aslist() \u65b9\u6cd5\u521b\u5efa\u6570\u7ec4\u3002\u8981\u8bbf\u95ee\u5143\u7d20\uff0c\u4f7f\u7528 names1[0]\u3002\u53ef\u4ee5\u4f7f\u7528\u589e\u5f3a\u578b for \u5faa\u73af\u6216\u4f20\u7edf for \u5faa\u73af\u904d\u5386\u6570\u7ec4\u3002\u8981\u4fee\u6539\u5143\u7d20\uff0c\u4f7f\u7528 names1[0] = &#8220;jane&#8221;\u3002java \u8fd8\u63d0\u4f9b\u4e86\u6570\u7ec4\u65b9\u6cd5\uff0c\u4f8b\u5982 arrays.sort() \u8fdb\u884c\u6392\u5e8f\u3001arrays.copyof() \u521b\u5efa\u526f\u672c\u4ee5\u53ca arrays.tostring() \u8f6c\u6362\u4e3a\u5b57\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111504362611700.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u5b57\u7b26\u4e32\u6570\u7ec4\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"java\u4e2d\u5b57\u7b26\u4e32\u6570\u7ec4\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u7f16\u5199<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u7f16\u5199\u5b57\u7b26\u4e32\u6570\u7ec4\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a String[] \u7684\u53d8\u91cf\uff0c\u7136\u540e\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u503c\u3002<\/p>\n<p><strong>\u65b9\u6cd5<\/strong><\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u65b9\u6cd5 1\uff1a\u9010\u4e00\u8d4b\u503c\nString[] names1 = new String[3];\nnames1[0] = \"John\";\nnames1[1] = \"Mary\";\nnames1[2] = \"Bob\";\n\n\/\/ \u65b9\u6cd5 2\uff1a\u6570\u7ec4\u521d\u59cb\u5316\u5217\u8868\nString[] names2 = new String[] {\"John\", \"Mary\", \"Bob\"};\n\n\/\/ \u65b9\u6cd5 3\uff1a\u4f7f\u7528 Arrays.asList()\nString[] names3 = Arrays.asList(\"John\", \"Mary\", \"Bob\").toArray(new String[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20<\/strong><\/p>\n<p>\u8981\u8bbf\u95ee\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>System.out.println(names1[0]); \/\/ \u8f93\u51fa \"John\"<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u904d\u5386\u6570\u7ec4<\/strong><\/p>\n<p>\u8981\u904d\u5386\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre>\/\/ \u589e\u5f3a\u578b for \u5faa\u73af\nfor (String name : names1) {\n    System.out.println(name);\n}\n\n\/\/ \u4f20\u7edf for \u5faa\u73af\nfor (int i = 0; i &lt; names1.length; i++) {\n    System.out.println(names1[i]);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4fee\u6539\u6570\u7ec4\u5143\u7d20<\/strong><\/p>\n<p>\u8981\u4fee\u6539\u5b57\u7b26\u4e32\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>names1[0] = \"Jane\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528\u6570\u7ec4\u65b9\u6cd5<\/strong><\/p>\n<p>Java \u63d0\u4f9b\u4e86\u8bb8\u591a\u53ef\u7528\u4e8e\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u5b9e\u7528\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>Arrays.sort()\uff1a\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/li>\n<li>Arrays.copyOf()\uff1a\u521b\u5efa\u6570\u7ec4\u7684\u526f\u672c<\/li>\n<li>Arrays.toString()\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u5199\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\u7f16\u5199\u5b57\u7b26\u4e32\u6570\u7ec4\u9700\u8981\u521b\u5efa\u4e00\u4e2a string[] \u7c7b\u578b\u53d8\u91cf\u5e76\u5206\u914d\u503c\u3002\u60a8\u53ef\u4ee5\u91c7\u7528\u9010\u4e00\u8d4b\u503c\u3001\u6570\u7ec4\u521d\u59cb\u5316\u5217\u8868\u6216 arrays.aslist() \u65b9\u6cd5\u521b\u5efa\u6570\u7ec4\u3002\u8981\u8bbf\u95ee\u5143\u7d20\uff0c\u4f7f\u7528 names1[0]\u3002\u53ef\u4ee5\u4f7f\u7528\u589e\u5f3a\u578b for \u5faa\u73af\u6216\u4f20\u7edf for \u5faa\u73af\u904d\u5386\u6570\u7ec4\u3002\u8981\u4fee\u6539\u5143\u7d20\uff0c\u4f7f\u7528 names1[0] = &#8220;jane&#8221;\u3002java \u8fd8\u63d0\u4f9b\u4e86\u6570\u7ec4\u65b9\u6cd5\uff0c\u4f8b\u5982 arrays.sort() \u8fdb\u884c\u6392\u5e8f\u3001arrays.copyof() \u521b\u5efa\u526f\u672c\u4ee5\u53ca arrays.tostring() \u8f6c\u6362\u4e3a\u5b57 Java \u4e2d\u7f16\u5199 \u5728 Java \u4e2d\uff0c\u7f16\u5199\u5b57\u7b26\u4e32\u6570\u7ec4\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a String[] \u7684\u53d8\u91cf\uff0c\u7136\u540e\u4e3a\u5176\u5206\u914d\u4e00\u4e2a\u503c\u3002 \u65b9\u6cd5 \u8981\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u65b9\u6cd5 1\uff1a\u9010\u4e00\u8d4b\u503c String[] names1 = new String[3]; names1[0] = &#8220;John&#8221;; names1[1] = &#8220;Mary&#8221;; names1[2] = &#8220;Bob&#8221;; \/\/ \u65b9\u6cd5 2\uff1a\u6570\u7ec4\u521d\u59cb\u5316\u5217\u8868 String[] names2 = new [&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-38658","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38658","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=38658"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38658\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}