{"id":35014,"date":"2024-11-26T16:25:06","date_gmt":"2024-11-26T08:25:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35014\/"},"modified":"2024-11-26T16:25:06","modified_gmt":"2024-11-26T08:25:06","slug":"%e6%80%8e%e4%b9%88%e7%94%a8java%e5%88%9b%e5%bb%ba%e4%ba%8c%e7%bb%b4%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35014\/","title":{"rendered":"\u600e\u4e48\u7528java\u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4\u5341\u5206\u7b80\u4fbf\uff1a\u58f0\u660e\u6570\u7ec4\u7c7b\u578b\uff08\u5982\uff1astring[][] names\uff09\u521d\u59cb\u5316\u6570\u7ec4\uff0c\u6307\u5b9a\u884c\u6570\u548c\u5217\u6570\uff08\u5982\uff1anames = new string3\uff09\u8bbf\u95ee\u548c\u8bbe\u7f6e\u5143\u7d20\uff08\u5982\uff1anames1 = &#8220;john&#8221;\uff09\u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\uff08\u5982\uff1anames.length \u8fd4\u56de\u884c\u6570\uff0cnames[0].length \u8fd4\u56de\u5217\u6570\uff09\u904d\u5386\u6570\u7ec4\uff08\u5982\uff1a\u4f7f\u7528\u5d4c\u5957\u5faa\u73af for (int i = 0; i <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110308214222673.jpg\" class=\"aligncenter\" title=\"\u600e\u4e48\u7528java\u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4\u63d2\u56fe\" alt=\"\u600e\u4e48\u7528java\u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4\u975e\u5e38\u7b80\u5355\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8\u6307\u5b9a\u7c7b\u578b\u7684\u591a\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u5982\u4f55\u5b9e\u73b0\uff1a<\/p>\n<p><strong>1. \u58f0\u660e\u6570\u7ec4\u7c7b\u578b\uff1a<\/strong><\/p>\n<pre>&lt;\u7c7b\u578b\u540d&gt;[][] \u6570\u7ec4\u540d;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d \u7c7b\u578b\u540d \u662f\u5305\u542b\u5728\u6570\u7ec4\u4e2d\u7684\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u793a\u4f8b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[][] names;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u521d\u59cb\u5316\u6570\u7ec4\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 new \u5173\u952e\u5b57\u521d\u59cb\u5316\u6570\u7ec4\u5e76\u6307\u5b9a\u7ef4\u5ea6\uff1a<\/p>\n<pre>\u6570\u7ec4\u540d = new \u7c7b\u578b\u540d[\u884c\u6570][\u5217\u6570];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>names = new String[3][2];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a 3 \u884c 2 \u5217\u7684 String \u7c7b\u578b\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<p><strong>3. \u8bbf\u95ee\u548c\u8bbe\u7f6e\u5143\u7d20\uff1a<\/strong><\/p>\n<p>\u8981\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>\u6570\u7ec4\u540d[\u884c\u7d22\u5f15][\u5217\u7d22\u5f15]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8981\u8bbe\u7f6e\u5143\u7d20\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>\u6570\u7ec4\u540d[\u884c\u7d22\u5f15][\u5217\u7d22\u5f15] = \u503c;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>names[1][0] = \"John\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 length \u5c5e\u6027\u83b7\u53d6\u6570\u7ec4\u7684\u7ef4\u5ea6\uff1a<\/p>\n<ul>\n<li>arrayName.length \u8fd4\u56de\u884c\u6570<\/li>\n<li>arrayName[0].length \u8fd4\u56de\u5217\u6570<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>System.out.println(\"\u884c\u6570\uff1a\" + names.length);\nSystem.out.println(\"\u5217\u6570\uff1a\" + names[0].length);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u904d\u5386\u6570\u7ec4\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff1a<\/p>\n<pre>for (int i = 0; i &lt; names.length; i++) {\n    for (int j = 0; j &lt; names[i].length; j++) {\n        \/\/ \u8bbf\u95ee\u6216\u64cd\u4f5c names[i][j]\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6c34\u679c\u540d\u79f0\u7684\u4e8c\u7ef4\u6570\u7ec4\u5e76\u6253\u5370\u5176\u5185\u5bb9\u7684\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n<pre>public class Main {\n    public static void main(String[] args) {\n        String[][] fruits = new String[2][3];\n\n        fruits[0][0] = \"Apple\";\n        fruits[0][1] = \"Banana\";\n        fruits[0][2] = \"Cherry\";\n        fruits[1][0] = \"Grape\";\n        fruits[1][1] = \"Orange\";\n        fruits[1][2] = \"Pineapple\";\n\n        for (String[] row : fruits) {\n            for (String fruit : row) {\n                System.out.print(fruit + \" \");\n            }\n            System.out.println();\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236\n<\/p><\/blockquote>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7528java\u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4\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\u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4\u5341\u5206\u7b80\u4fbf\uff1a\u58f0\u660e\u6570\u7ec4\u7c7b\u578b\uff08\u5982\uff1astring[][] names\uff09\u521d\u59cb\u5316\u6570\u7ec4\uff0c\u6307\u5b9a\u884c\u6570\u548c\u5217\u6570\uff08\u5982\uff1anames = new string3\uff09\u8bbf\u95ee\u548c\u8bbe\u7f6e\u5143\u7d20\uff08\u5982\uff1anames1 = &#8220;john&#8221;\uff09\u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\uff08\u5982\uff1anames.length \u8fd4\u56de\u884c\u6570\uff0cnames[0].length \u8fd4\u56de\u5217\u6570\uff09\u904d\u5386\u6570\u7ec4\uff08\u5982\uff1a\u4f7f\u7528\u5d4c\u5957\u5faa\u73af for (int i = 0; i \u5982\u4f55\u4f7f\u7528 Java \u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4 \u5728 Java \u4e2d\u521b\u5efa\u4e8c\u7ef4\u5bf9\u8c61\u6570\u7ec4\u975e\u5e38\u7b80\u5355\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8\u6307\u5b9a\u7c7b\u578b\u7684\u591a\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u5982\u4f55\u5b9e\u73b0\uff1a 1. \u58f0\u660e\u6570\u7ec4\u7c7b\u578b\uff1a &lt;\u7c7b\u578b\u540d&gt;[][] \u6570\u7ec4\u540d; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d \u7c7b\u578b\u540d \u662f\u5305\u542b\u5728\u6570\u7ec4\u4e2d\u7684\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u793a\u4f8b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[][] names; \u767b\u5f55\u540e\u590d\u5236 2. \u521d\u59cb\u5316\u6570\u7ec4\uff1a \u4f7f\u7528 new \u5173\u952e\u5b57\u521d\u59cb\u5316\u6570\u7ec4\u5e76\u6307\u5b9a\u7ef4\u5ea6\uff1a \u6570\u7ec4\u540d = new \u7c7b\u578b\u540d[\u884c\u6570][\u5217\u6570]; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b\uff1a names = new String[3][2]; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a 3 \u884c 2 [&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-35014","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35014","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=35014"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35014\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}