{"id":35303,"date":"2024-11-26T13:35:24","date_gmt":"2024-11-26T05:35:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35303\/"},"modified":"2024-11-26T13:35:24","modified_gmt":"2024-11-26T05:35:24","slug":"java%e6%80%8e%e4%b9%88%e8%bf%94%e5%9b%9e%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%95%b0%e7%bb%84%e7%9a%84%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35303\/","title":{"rendered":"java\u600e\u4e48\u8fd4\u56de\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u83b7\u53d6\u5b57\u7b26\u4e32\u6570\u7ec4\u957f\u5ea6\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 length \u53d8\u91cf\uff1b\u4f7f\u7528 length() \u65b9\u6cd5\uff1b\u4f7f\u7528 for \u5faa\u73af\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110323514211397.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u8fd4\u56de\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6\u63d2\u56fe\" alt=\"java\u600e\u4e48\u8fd4\u56de\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u83b7\u53d6\u957f\u5ea6<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 length \u53d8\u91cf<\/strong><\/p>\n<p>\u6bcf\u4e2a\u6570\u7ec4\u90fd\u6709\u4e00\u4e2a\u5185\u7f6e\u7684 length \u53d8\u91cf\uff0c\u5b83\u5b58\u50a8\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u83b7\u53d6\u5176\u957f\u5ea6\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] strArray = {\"Hello\", \"World\", \"!\"};\nint length = strArray.length;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0clength \u53d8\u91cf\u7684\u503c\u4e3a 3\uff0c\u56e0\u4e3a\u5b83\u8868\u793a\u6570\u7ec4\u4e2d\u5b57\u7b26\u4e32\u7684\u6570\u91cf\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 length() \u65b9\u6cd5<\/strong><\/p>\n<p>\u5b57\u7b26\u4e32\u6570\u7ec4\u4e5f\u53ef\u4ee5\u88ab\u89c6\u4e3a List\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528 List \u63a5\u53e3\u7684 length() \u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\uff1a<\/p>\n<pre>String[] strArray = {\"Hello\", \"World\", \"!\"};\nint length = strArray.length();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0e length \u53d8\u91cf\u7c7b\u4f3c\uff0clength() \u65b9\u6cd5\u4e5f\u8fd4\u56de\u6570\u7ec4\u4e2d\u5b57\u7b26\u4e32\u7684\u6570\u91cf\u3002<\/p>\n<p><strong>3. \u4f7f\u7528 for \u5faa\u73af<\/strong><\/p>\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u6765\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528\u8ba1\u6570\u5668\u6765\u8ba1\u7b97\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u6570\u91cf\uff1a<\/p>\n<pre>String[] strArray = {\"Hello\", \"World\", \"!\"};\nint length = 0;\n\nfor (String str : strArray) {\n    length++;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0clength \u53d8\u91cf\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u589e\u52a0\u4e00\u6b21\uff0c\u4ece\u800c\u8ba1\u7b97\u51fa\u6570\u7ec4\u4e2d\u7684\u5b57\u7b26\u4e32\u6570\u91cf\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u5728 Java \u4e2d\u83b7\u53d6\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6\u6709\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1alength \u53d8\u91cf\u3001length() \u65b9\u6cd5\u548c for \u5faa\u73af\u3002\u6700\u7b80\u5355\u3001\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 length \u53d8\u91cf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u8fd4\u56de\u7684\u957f\u5ea6\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\u83b7\u53d6\u5b57\u7b26\u4e32\u6570\u7ec4\u957f\u5ea6\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 length \u53d8\u91cf\uff1b\u4f7f\u7528 length() \u65b9\u6cd5\uff1b\u4f7f\u7528 for \u5faa\u73af\u3002 Java \u4e2d\u83b7\u53d6\u957f\u5ea6 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u957f\u5ea6\uff1a 1. \u4f7f\u7528 length \u53d8\u91cf \u6bcf\u4e2a\u6570\u7ec4\u90fd\u6709\u4e00\u4e2a\u5185\u7f6e\u7684 length \u53d8\u91cf\uff0c\u5b83\u5b58\u50a8\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u83b7\u53d6\u5176\u957f\u5ea6\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] strArray = {&#8220;Hello&#8221;, &#8220;World&#8221;, &#8220;!&#8221;}; int length = strArray.length; \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0clength \u53d8\u91cf\u7684\u503c\u4e3a 3\uff0c\u56e0\u4e3a\u5b83\u8868\u793a\u6570\u7ec4\u4e2d\u5b57\u7b26\u4e32\u7684\u6570\u91cf\u3002 2. \u4f7f\u7528 length() \u65b9\u6cd5 \u5b57\u7b26\u4e32\u6570\u7ec4\u4e5f\u53ef\u4ee5\u88ab\u89c6\u4e3a List\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528 List \u63a5\u53e3\u7684 length() \u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u957f\u5ea6\uff1a String[] strArray = {&#8220;Hello&#8221;, &#8220;World&#8221;, &#8220;!&#8221;}; int length = [&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-35303","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35303","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=35303"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35303\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}