{"id":66972,"date":"2025-05-03T15:39:59","date_gmt":"2025-05-03T07:39:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66972\/"},"modified":"2025-05-03T15:39:59","modified_gmt":"2025-05-03T07:39:59","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e5%88%a4%e6%96%ad%e4%b8%80%e4%b8%aa%e6%95%b0%e7%bb%84%e4%b8%ba%e7%a9%ba-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66972\/","title":{"rendered":"Java\u4e2d\u600e\u4e48\u5224\u65ad\u4e00\u4e2a\u6570\u7ec4\u4e3a\u7a7a"},"content":{"rendered":"<blockquote><p>\n  \u8981\u5728 java \u4e2d\u5224\u65ad\u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u68c0\u67e5\u6570\u7ec4\u7684\u957f\u5ea6\uff1b\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\uff0c\u4f7f\u7528 isempty() \u65b9\u6cd5\uff1b\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u6570\u7ec4\uff0c\u4f7f\u7528 objects.isnull() \u65b9\u6cd5\uff1b\u5728 java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 optional.ofnullable() \u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111721272493744.jpg\" class=\"aligncenter\" title=\"Java\u4e2d\u600e\u4e48\u5224\u65ad\u4e00\u4e2a\u6570\u7ec4\u4e3a\u7a7a\u63d2\u56fe\" alt=\"Java\u4e2d\u600e\u4e48\u5224\u65ad\u4e00\u4e2a\u6570\u7ec4\u4e3a\u7a7a\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u5224\u65ad\u6570\u7ec4\u4e3a\u7a7a<\/strong><\/p>\n<p>\u8981\u5224\u65ad\u4e00\u4e2a Java \u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u68c0\u67e5\u6570\u7ec4\u7684\u957f\u5ea6<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u68c0\u67e5\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u5426\u4e3a 0\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>if (array.length == 0) {\n  \/\/ \u6570\u7ec4\u4e3a\u7a7a\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 isEmpty() \u65b9\u6cd5\uff08\u5982\u679c\u8be5\u6570\u7ec4\u662f Primitive \u6570\u7ec4\uff09<\/strong><\/p>\n<p>\u5bf9\u4e8e int[]\u3001float[] \u7b49\u539f\u59cb\u7c7b\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 isEmpty() \u65b9\u6cd5\uff1a<\/p>\n<pre>if (Arrays.isEmpty(array)) {\n  \/\/ \u6570\u7ec4\u4e3a\u7a7a\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Objects.isNull() \u65b9\u6cd5\uff08\u9002\u7528\u4e8e\u5f15\u7528\u7c7b\u578b\u6570\u7ec4\uff09<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u6570\u7ec4\uff08\u5982 String[]\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 Objects.isNull() \u65b9\u6cd5\uff1a<\/p>\n<pre>if (Objects.isNull(array)) {\n  \/\/ \u6570\u7ec4\u4e3a\u7a7a\n} else if (array.length == 0) {\n  \/\/ \u6570\u7ec4\u975e\u7a7a\u4f46\u6ca1\u6709\u5143\u7d20\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 Optional.ofNullable() \u65b9\u6cd5\uff08\u7248\u672c Java 8 \u4ee5\u4e0a\uff09<\/strong><\/p>\n<p>Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Optional.ofNullable() \u65b9\u6cd5\uff1a<\/p>\n<pre>Optional&lt;String[]&gt; arrayAsOptional = Optional.ofNullable(array);\nif (!arrayAsOptional.isPresent()) {\n  \/\/ \u6570\u7ec4\u4e3a\u7a7a\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6839\u636e\u6570\u7ec4\u7684\u7c7b\u578b\u548c\u504f\u597d\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava\u4e2d\u600e\u4e48\u5224\u65ad\u4e00\u4e2a\u6570\u7ec4\u4e3a\u7a7a\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>\u8981\u5728 java \u4e2d\u5224\u65ad\u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u68c0\u67e5\u6570\u7ec4\u7684\u957f\u5ea6\uff1b\u5bf9\u4e8e\u57fa\u672c\u7c7b\u578b\u6570\u7ec4\uff0c\u4f7f\u7528 isempty() \u65b9\u6cd5\uff1b\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u6570\u7ec4\uff0c\u4f7f\u7528 objects.isnull() \u65b9\u6cd5\uff1b\u5728 java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 optional.ofnullable() \u65b9\u6cd5\u3002 \u5982\u4f55\u5728 Java \u4e2d\u5224\u65ad\u6570\u7ec4\u4e3a\u7a7a \u8981\u5224\u65ad\u4e00\u4e2a Java \u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. \u68c0\u67e5\u6570\u7ec4\u7684\u957f\u5ea6 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u68c0\u67e5\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u5426\u4e3a 0\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b if (array.length == 0) { \/\/ \u6570\u7ec4\u4e3a\u7a7a } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 isEmpty() \u65b9\u6cd5\uff08\u5982\u679c\u8be5\u6570\u7ec4\u662f Primitive \u6570\u7ec4\uff09 \u5bf9\u4e8e int[]\u3001float[] \u7b49\u539f\u59cb\u7c7b\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 isEmpty() \u65b9\u6cd5\uff1a if (Arrays.isEmpty(array)) { \/\/ \u6570\u7ec4\u4e3a\u7a7a } \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528 Objects.isNull() \u65b9\u6cd5\uff08\u9002\u7528\u4e8e\u5f15\u7528\u7c7b\u578b\u6570\u7ec4\uff09 \u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\u6570\u7ec4\uff08\u5982 [&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-66972","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66972","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=66972"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66972\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}