{"id":37784,"date":"2024-11-26T10:43:09","date_gmt":"2024-11-26T02:43:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37784\/"},"modified":"2024-11-26T10:43:09","modified_gmt":"2024-11-26T02:43:09","slug":"java%e7%9a%84%e5%ad%97%e7%ac%a6%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e8%be%93%e5%85%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37784\/","title":{"rendered":"java\u7684\u5b57\u7b26\u6570\u7ec4\u600e\u4e48\u8f93\u5165"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u8f93\u5165\u5b57\u7b26\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\u76f4\u63a5\u4f7f\u7528 char[] \u6570\u7ec4\u3001\u5229\u7528 string.tochararray() \u8f6c\u6362 string \u5bf9\u8c61\u3001\u901a\u8fc7 scanner \u4ece\u63a7\u5236\u53f0\u8f93\u5165\u3001\u8fd0\u7528 bufferedreader \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\uff0c\u4ee5\u53ca\u4f7f\u7528 character.tochars()\u3001arrays.copyof()\u3001stream api \u7b49\u5176\u4ed6\u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111310282040188.jpg\" class=\"aligncenter\" title=\"java\u7684\u5b57\u7b26\u6570\u7ec4\u600e\u4e48\u8f93\u5165\u63d2\u56fe\" alt=\"java\u7684\u5b57\u7b26\u6570\u7ec4\u600e\u4e48\u8f93\u5165\u63d2\u56fe\" \/><\/p>\n<h2>\u5982\u4f55\u5728 Java \u4e2d\u8f93\u5165\u5b57\u7b26\u6570\u7ec4<\/h2>\n<p><strong>\u76f4\u63a5\u4f7f\u7528 char[] \u6570\u7ec4<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528 char[] \u6570\u7ec4\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>char[] myChars = new char[]{'a', 'b', 'c', 'd', 'e'};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 String.toCharArray()<\/strong><\/p>\n<p>\u5982\u679c\u5df2\u6709\u4e00\u4e2a String \u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528 toCharArray() \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String myString = \"abcdef\";\nchar[] myChars = myString.toCharArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u901a\u8fc7 Scanner \u8f93\u5165<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7 Scanner \u4ece\u63a7\u5236\u53f0\u8f93\u5165\u5b57\u7b26\u6570\u7ec4\uff1a<\/p>\n<pre>Scanner scanner = new Scanner(System.in);\nSystem.out.println(\"\u8f93\u5165\u5b57\u7b26\u6570\u7ec4\uff1a\");\nchar[] myChars = scanner.nextLine().toCharArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 BufferedReader<\/strong><\/p>\n<p>BufferedReader \u4e5f\u53ef\u7528\u4e8e\u4ece\u6587\u4ef6\u4e2d\u8f93\u5165\u5b57\u7b26\u6570\u7ec4\uff1a<\/p>\n<pre>BufferedReader reader = new BufferedReader(new FileReader(\"file.txt\"));\nchar[] myChars = reader.readLine().toCharArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5<\/strong><\/p>\n<p>\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u8f93\u5165\u5b57\u7b26\u6570\u7ec4\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 Character.toChars() \u4ece int \u503c\u521b\u5efa\u5b57\u7b26\u6570\u7ec4<\/li>\n<li>\u4f7f\u7528 Arrays.copyOf() \u4ece\u53e6\u4e00\u4e2a\u6570\u7ec4\u521b\u5efa\u5b57\u7b26\u6570\u7ec4<\/li>\n<li>\u4f7f\u7528 Stream API \u4ece\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u5b57\u7b26\u5e8f\u5217\u521b\u5efa\u5b57\u7b26\u6570\u7ec4<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u7684\u5b57\u7b26\u6570\u7ec4\u600e\u4e48\u8f93\u5165\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\u8f93\u5165\u5b57\u7b26\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\u76f4\u63a5\u4f7f\u7528 char[] \u6570\u7ec4\u3001\u5229\u7528 string.tochararray() \u8f6c\u6362 string \u5bf9\u8c61\u3001\u901a\u8fc7 scanner \u4ece\u63a7\u5236\u53f0\u8f93\u5165\u3001\u8fd0\u7528 bufferedreader \u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\uff0c\u4ee5\u53ca\u4f7f\u7528 character.tochars()\u3001arrays.copyof()\u3001stream api \u7b49\u5176\u4ed6\u65b9\u6cd5\u3002 \u5982\u4f55\u5728 Java \u4e2d\u8f93\u5165\u5b57\u7b26\u6570\u7ec4 \u76f4\u63a5\u4f7f\u7528 char[] \u6570\u7ec4 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528 char[] \u6570\u7ec4\uff0c\u5982\u4e0b\u6240\u793a\uff1a char[] myChars = new char[]{&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;, &#8216;d&#8217;, &#8216;e&#8217;}; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 String.toCharArray() \u5982\u679c\u5df2\u6709\u4e00\u4e2a String \u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528 toCharArray() \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String myString = &#8220;abcdef&#8221;; char[] myChars = myString.toCharArray(); \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7 Scanner \u8f93\u5165 [&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-37784","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37784","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=37784"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37784\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}