{"id":65749,"date":"2025-05-03T10:56:06","date_gmt":"2025-05-03T02:56:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65749\/"},"modified":"2025-05-03T10:56:06","modified_gmt":"2025-05-03T02:56:06","slug":"java%e6%80%8e%e4%b9%88%e7%94%a8%e6%95%b0%e7%bb%84%e5%ad%98%e6%94%be%e5%a7%93%e5%90%8d-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65749\/","title":{"rendered":"java\u600e\u4e48\u7528\u6570\u7ec4\u5b58\u653e\u59d3\u540d"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 java \u6570\u7ec4\u5b58\u653e\u59d3\u540d\u9700\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u58f0\u660e string \u6570\u7ec4\u3002\u4f7f\u7528\u4e0b\u6807\u586b\u5145\u6570\u7ec4\u5143\u7d20\u3002\u4f7f\u7528\u4e0b\u6807\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002\u4f7f\u7528 for \u5faa\u73af\u6216\u589e\u5f3a-for \u5faa\u73af\u904d\u5386\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111309091853656.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u7528\u6570\u7ec4\u5b58\u653e\u59d3\u540d\u63d2\u56fe\" alt=\"java\u600e\u4e48\u7528\u6570\u7ec4\u5b58\u653e\u59d3\u540d\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u5b58\u653e\u59d3\u540d<\/strong><\/p>\n<p>Java \u4e2d\u4f7f\u7528\u6570\u7ec4\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u50a8\u591a\u4e2a\u503c\uff0c\u5305\u62ec\u59d3\u540d\u3002\u8981\u4f7f\u7528\u6570\u7ec4\u5b58\u653e\u59d3\u540d\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<p><strong>1. \u58f0\u660e\u6570\u7ec4<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u58f0\u660e\u4e00\u4e2a String \u7c7b\u578b\u7684\u6570\u7ec4\u6765\u5b58\u50a8\u59d3\u540d\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u58f0\u660e\u4e00\u4e2a\u80fd\u5bb9\u7eb3 10 \u4e2a\u59d3\u540d\u7684\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] names = new String[10];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u586b\u5145\u6570\u7ec4<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u6570\u7ec4\u4e0b\u6807\u6765\u586b\u5145\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u59d3\u540d\uff1a<\/p>\n<pre>names[0] = \"John Doe\";\nnames[1] = \"Jane Smith\";\nnames[2] = \"Mark Jones\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u8bbf\u95ee\u6570\u7ec4\u5143\u7d20<\/strong><\/p>\n<p>\u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u4e0b\u6807\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6253\u5370\u51fa\u6570\u7ec4\u4e2d\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff08Jane Smith\uff09\uff1a<\/p>\n<pre>System.out.println(names[1]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u904d\u5386\u6570\u7ec4<\/strong><\/p>\n<p>\u8981\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u6216\u589e\u5f3a-for \u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u904d\u5386\u6570\u7ec4\u5e76\u6253\u5370\u51fa\u6bcf\u4e2a\u59d3\u540d\uff1a<\/p>\n<p><strong>for \u5faa\u73af\uff1a<\/strong><\/p>\n<pre>for (int i = 0; i &lt; names.length; i++) {\n  System.out.println(names[i]);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u589e\u5f3a-for \u5faa\u73af\uff1a<\/strong><\/p>\n<pre>for (String name : names) {\n  System.out.println(name);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>\u6570\u7ec4\u7684\u5927\u5c0f\u5728\u521b\u5efa\u65f6\u88ab\u56fa\u5b9a\uff0c\u56e0\u6b64\u5728\u58f0\u660e\u6570\u7ec4\u65f6\u9700\u8981\u8003\u8651\u9700\u8981\u7684\u5bb9\u91cf\u3002<\/li>\n<li>\u6570\u7ec4\u7684\u4e0b\u6807\u662f\u4ece 0 \u5f00\u59cb\u7684\uff0c\u56e0\u6b64\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u4e0b\u6807\u5c06\u662f\u6570\u7ec4\u957f\u5ea6\u51cf 1\u3002<\/li>\n<li>\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u6570\u7ec4\u5143\u7d20\u4f1a\u5bfc\u81f4 <strong>ArrayIndexOutOfBoundsException<\/strong>\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u7528\u6570\u7ec4\u5b58\u653e\u59d3\u540d\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 java \u6570\u7ec4\u5b58\u653e\u59d3\u540d\u9700\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u58f0\u660e string \u6570\u7ec4\u3002\u4f7f\u7528\u4e0b\u6807\u586b\u5145\u6570\u7ec4\u5143\u7d20\u3002\u4f7f\u7528\u4e0b\u6807\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002\u4f7f\u7528 for \u5faa\u73af\u6216\u589e\u5f3a-for \u5faa\u73af\u904d\u5386\u6570\u7ec4\u3002 Java \u6570\u7ec4\u5b58\u653e\u59d3\u540d Java \u4e2d\u4f7f\u7528\u6570\u7ec4\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u50a8\u591a\u4e2a\u503c\uff0c\u5305\u62ec\u59d3\u540d\u3002\u8981\u4f7f\u7528\u6570\u7ec4\u5b58\u653e\u59d3\u540d\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a 1. \u58f0\u660e\u6570\u7ec4 \u9996\u5148\uff0c\u58f0\u660e\u4e00\u4e2a String \u7c7b\u578b\u7684\u6570\u7ec4\u6765\u5b58\u50a8\u59d3\u540d\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u58f0\u660e\u4e00\u4e2a\u80fd\u5bb9\u7eb3 10 \u4e2a\u59d3\u540d\u7684\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] names = new String[10]; \u767b\u5f55\u540e\u590d\u5236 2. \u586b\u5145\u6570\u7ec4 \u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u6570\u7ec4\u4e0b\u6807\u6765\u586b\u5145\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5411\u6570\u7ec4\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u59d3\u540d\uff1a names[0] = &#8220;John Doe&#8221;; names[1] = &#8220;Jane Smith&#8221;; names[2] = &#8220;Mark Jones&#8221;; \u767b\u5f55\u540e\u590d\u5236 3. \u8bbf\u95ee\u6570\u7ec4\u5143\u7d20 \u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u4e0b\u6807\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6253\u5370\u51fa\u6570\u7ec4\u4e2d\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff08Jane Smith\uff09\uff1a System.out.println(names[1]); \u767b\u5f55\u540e\u590d\u5236 4. \u904d\u5386\u6570\u7ec4 \u8981\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u6216\u589e\u5f3a-for \u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u904d\u5386\u6570\u7ec4\u5e76\u6253\u5370\u51fa\u6bcf\u4e2a\u59d3\u540d\uff1a for [&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-65749","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65749","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=65749"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65749\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}