{"id":37732,"date":"2024-11-26T08:06:18","date_gmt":"2024-11-26T00:06:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37732\/"},"modified":"2024-11-26T08:06:18","modified_gmt":"2024-11-26T00:06:18","slug":"java%e5%ad%97%e7%ac%a6%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e8%bd%acint","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37732\/","title":{"rendered":"java\u5b57\u7b26\u6570\u7ec4\u600e\u4e48\u8f6cint"},"content":{"rendered":"<blockquote><p>\n  \u5b57\u7b26\u6570\u7ec4\u8f6cint\u65b9\u6cd5\u6709\u56db\u79cd\uff1a\u4f7f\u7528integer.valueof()\u548cstring.valueof()\u3001\u4f7f\u7528integer.parseint()\u3001\u4f7f\u7528character.getnumericvalue()\u5408\u5e76\u6570\u5b57\u503c\uff0c\u6216\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u7d2f\u52a0\u6570\u5b57\uff08\u4ec5\u9650\u5341\u8fdb\u5236\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111309361924700.jpg\" class=\"aligncenter\" title=\"java\u5b57\u7b26\u6570\u7ec4\u600e\u4e48\u8f6cint\u63d2\u56fe\" alt=\"java\u5b57\u7b26\u6570\u7ec4\u600e\u4e48\u8f6cint\u63d2\u56fe\" \/><\/p>\n<p><strong>Java\u4e2d\u5b57\u7b26\u6570\u7ec4\u8f6cint<\/strong><\/p>\n<p>\u8981\u5c06Java\u4e2d\u7684\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362\u4e3aint\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u521b\u5efaInteger\u5bf9\u8c61<\/strong><\/p>\n<pre>Integer num = Integer.valueOf(String.valueOf(arr));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2darr\u8868\u793a\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>2. \u4f7f\u7528Integer\u7c7b\u7684parseInt()\u65b9\u6cd5<\/strong><\/p>\n<pre>Integer num = Integer.parseInt(new String(arr));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528Character\u7c7b\u7684getNumericValue()\u65b9\u6cd5<\/strong><\/p>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528Character.getNumericValue()\u65b9\u6cd5\u63d0\u53d6\u5176\u6570\u5b57\u503c\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u503c\u5408\u5e76\u4e3a\u4e00\u4e2aint\u3002<\/p>\n<pre>int num = 0;\nfor (char c : arr) {\n    num *= 10;\n    num += Character.getNumericValue(c);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\uff08\u4ec5\u9650\u4e8e\u5341\u8fdb\u5236\u6570\u5b57\uff09<\/strong><\/p>\n<p>\u5982\u679c\u5b57\u7b26\u6570\u7ec4\u8868\u793a\u5341\u8fdb\u5236\u6570\u5b57\uff0c\u53ef\u4ee5\u9010\u4e2a\u904d\u5386\u5b57\u7b26\uff0c\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u5c06\u5176\u7d2f\u52a0\u5230int\u503c\u4e2d\u3002<\/p>\n<pre>int num = 0;\nfor (char c : arr) {\n    num = num * 10 + (c - '0');\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>char[] arr = {'1', '2', '3'};\n\n\/\/ \u65b9\u6cd5 1\nint num1 = Integer.valueOf(String.valueOf(arr));\n\n\/\/ \u65b9\u6cd5 2\nint num2 = Integer.parseInt(new String(arr));\n\n\/\/ \u65b9\u6cd5 3\nint num3 = 0;\nfor (char c : arr) {\n    num3 *= 10;\n    num3 += Character.getNumericValue(c);\n}\n\n\/\/ \u65b9\u6cd5 4\nint num4 = 0;\nfor (char c : arr) {\n    num4 = num4 * 10 + (c - '0');\n}\n\nSystem.out.println(num1); \/\/ 123\nSystem.out.println(num2); \/\/ 123\nSystem.out.println(num3); \/\/ 123\nSystem.out.println(num4); \/\/ 123<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u5b57\u7b26\u6570\u7ec4\u600e\u4e48\u8f6cint\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>\u5b57\u7b26\u6570\u7ec4\u8f6cint\u65b9\u6cd5\u6709\u56db\u79cd\uff1a\u4f7f\u7528integer.valueof()\u548cstring.valueof()\u3001\u4f7f\u7528integer.parseint()\u3001\u4f7f\u7528character.getnumericvalue()\u5408\u5e76\u6570\u5b57\u503c\uff0c\u6216\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u7d2f\u52a0\u6570\u5b57\uff08\u4ec5\u9650\u5341\u8fdb\u5236\uff09\u3002 Java\u4e2d\u5b57\u7b26\u6570\u7ec4\u8f6cint \u8981\u5c06Java\u4e2d\u7684\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362\u4e3aint\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a 1. \u521b\u5efaInteger\u5bf9\u8c61 Integer num = Integer.valueOf(String.valueOf(arr)); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2darr\u8868\u793a\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 2. \u4f7f\u7528Integer\u7c7b\u7684parseInt()\u65b9\u6cd5 Integer num = Integer.parseInt(new String(arr)); \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528Character\u7c7b\u7684getNumericValue()\u65b9\u6cd5 \u5bf9\u4e8e\u6bcf\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528Character.getNumericValue()\u65b9\u6cd5\u63d0\u53d6\u5176\u6570\u5b57\u503c\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u503c\u5408\u5e76\u4e3a\u4e00\u4e2aint\u3002 int num = 0; for (char c : arr) { num *= 10; num += Character.getNumericValue(c); } \u767b\u5f55\u540e\u590d\u5236 4. \u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\uff08\u4ec5\u9650\u4e8e\u5341\u8fdb\u5236\u6570\u5b57\uff09 \u5982\u679c\u5b57\u7b26\u6570\u7ec4\u8868\u793a\u5341\u8fdb\u5236\u6570\u5b57\uff0c\u53ef\u4ee5\u9010\u4e2a\u904d\u5386\u5b57\u7b26\uff0c\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u5c06\u5176\u7d2f\u52a0\u5230int\u503c\u4e2d\u3002 int num = 0; for (char c : arr) { num [&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-37732","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37732","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=37732"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37732\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}