{"id":34806,"date":"2024-11-26T15:47:09","date_gmt":"2024-11-26T07:47:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34806\/"},"modified":"2024-11-26T15:47:09","modified_gmt":"2024-11-26T07:47:09","slug":"java%e4%b8%ad%e6%80%8e%e4%b9%88%e5%8f%96%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34806\/","title":{"rendered":"java\u4e2d\u600e\u4e48\u53d6\u6570\u7ec4\u4e2d\u7684\u6570\u5b57"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u6570\u5b57\u5305\u88c5\u7c7b\u578b\u53ef\u4ece java \u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u5b57\u30021. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6570\u5b57\u6a21\u5f0f\u30022. \u4f7f\u7528\u6570\u5b57\u5305\u88c5\u7c7b\u578b\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u8f6c\u6362\u4e3a\u6570\u5b57\u5bf9\u8c61\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110303363741465.jpg\" class=\"aligncenter\" title=\"java\u4e2d\u600e\u4e48\u53d6\u6570\u7ec4\u4e2d\u7684\u6570\u5b57\u63d2\u56fe\" alt=\"java\u4e2d\u600e\u4e48\u53d6\u6570\u7ec4\u4e2d\u7684\u6570\u5b57\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u5b57<\/strong><\/p>\n<p>\u4ece Java \u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u5b57\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u901a\u8fc7\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f60\u53ef\u4ee5\u627e\u5230\u6570\u7ec4\u4e2d\u6240\u6709\u5339\u914d\u6570\u5b57\u6a21\u5f0f\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] array = {\"Hello\", \"123\", \"World\", \"456\"};\n\n\/\/ \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6570\u5b57\nPattern pattern = Pattern.compile(\"\\d+\");\nMatcher matcher = pattern.matcher(Arrays.toString(array));\n\n\/\/ \u6253\u5370\u5339\u914d\u7684\u6570\u5b57\nwhile (matcher.find()) {\n    System.out.println(matcher.group());\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>123\n456<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528\u6570\u5b57\u5305\u88c5\u7c7b\u578b<\/strong><\/p>\n<p>Java \u4e2d\u7684\u6570\u5b57\u5305\u88c5\u7c7b\u578b\uff08\u5982 Integer\u3001Double \u7b49\uff09\u63d0\u4f9b\u4e86\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u8f6c\u6362\u4e3a\u6570\u5b57\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String[] array = {\"Hello\", \"123\", \"World\", \"456\"};\n\n\/\/ \u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u8f6c\u6362\u4e3a\u6570\u5b57\u5bf9\u8c61\nInteger[] numbers = new Integer[array.length];\nfor (int i = 0; i &lt; array.length; i++) {\n    try {\n        numbers[i] = Integer.valueOf(array[i]);\n    } catch (NumberFormatException e) {\n        \/\/ \u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u662f\u6570\u5b57\uff0c\u5219\u8df3\u8fc7\u5143\u7d20\n        continue;\n    }\n}\n\n\/\/ \u6253\u5370\u8f6c\u6362\u540e\u7684\u6570\u5b57\nfor (Integer number : numbers) {\n    if (number != null) {\n        System.out.println(number);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>123\n456<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u6765\u4ece Java \u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u5b57\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2d\u600e\u4e48\u53d6\u6570\u7ec4\u4e2d\u7684\u6570\u5b57\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>\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u6570\u5b57\u5305\u88c5\u7c7b\u578b\u53ef\u4ece java \u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u5b57\u30021. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6570\u5b57\u6a21\u5f0f\u30022. \u4f7f\u7528\u6570\u5b57\u5305\u88c5\u7c7b\u578b\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u8f6c\u6362\u4e3a\u6570\u5b57\u5bf9\u8c61\u3002 \u5982\u4f55\u4ece Java \u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u5b57 \u4ece Java \u6570\u7ec4\u4e2d\u63d0\u53d6\u6570\u5b57\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u901a\u8fc7\u4e24\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\uff1a \u65b9\u6cd5 1\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f60\u53ef\u4ee5\u627e\u5230\u6570\u7ec4\u4e2d\u6240\u6709\u5339\u914d\u6570\u5b57\u6a21\u5f0f\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] array = {&#8220;Hello&#8221;, &#8220;123&#8221;, &#8220;World&#8221;, &#8220;456&#8221;}; \/\/ \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6570\u5b57 Pattern pattern = Pattern.compile(&#8220;\\d+&#8221;); Matcher matcher = pattern.matcher(Arrays.toString(array)); \/\/ \u6253\u5370\u5339\u914d\u7684\u6570\u5b57 while (matcher.find()) { System.out.println(matcher.group()); } \u767b\u5f55\u540e\u590d\u5236 \u8f93\u51fa\uff1a 123 456 \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528\u6570\u5b57\u5305\u88c5\u7c7b\u578b Java \u4e2d\u7684\u6570\u5b57\u5305\u88c5\u7c7b\u578b\uff08\u5982 Integer\u3001Double \u7b49\uff09\u63d0\u4f9b\u4e86\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u8f6c\u6362\u4e3a\u6570\u5b57\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a String[] array = {&#8220;Hello&#8221;, [&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-34806","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34806","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=34806"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34806\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}