{"id":275153,"date":"2024-05-16T14:20:35","date_gmt":"2024-05-16T06:20:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/275153.html"},"modified":"2024-05-16T14:20:54","modified_gmt":"2024-05-16T06:20:54","slug":"java-%e9%a1%b9%e7%9b%ae%e4%b8%ad%e6%80%8e%e4%b9%88%e5%b0%86%e6%95%b0%e7%bb%84%e8%bd%ac%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/275153.html","title":{"rendered":"java \u9879\u76ee\u4e2d\u600e\u4e48\u5c06\u6570\u7ec4\u8f6c\u4e3a\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200234\/d2902656-beb3-46d7-9cb5-a1948ecfbea7.webp\" alt=\"java \u9879\u76ee\u4e2d\u600e\u4e48\u5c06\u6570\u7ec4\u8f6c\u4e3a\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p>\u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u5728Java\u9879\u76ee\u4e2d\u662f\u4e00\u9879\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<strong>\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0<\/strong>\uff0c\u5305\u62ec\u4f7f\u7528<code>Arrays.toString()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528Java 8\u7684<code>Stream API<\/code>\u3001\u5229\u7528<code>StringBuilder<\/code>\u4ee5\u53ca\u91c7\u7528<code>String.join()<\/code>\u65b9\u6cd5\u3002<strong>\u6700\u76f4\u89c2\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>Arrays.toString()<\/code>\u65b9\u6cd5<\/strong>\uff0c\u5b83\u80fd\u591f\u76f4\u63a5\u5c06\u6570\u7ec4\u5143\u7d20\u8f6c\u5316\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4f1a\u88ab\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u5e76\u7531\u9017\u53f7\u548c\u7a7a\u683c\u9694\u5f00\uff0c\u6700\u7ec8\u5305\u88f9\u5728\u65b9\u62ec\u53f7\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u5c24\u5176\u9002\u5408\u4e8e\u5728\u65e5\u5fd7\u8bb0\u5f55\u6216\u8005\u9700\u8981\u5feb\u901f\u67e5\u770b\u6570\u7ec4\u5185\u5bb9\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>Arrays.toString()<\/code><\/h3>\n<\/p>\n<p><p>Java\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u4fbf\u5229\u7684\u5de5\u5177\u7c7b<code>Arrays<\/code>\uff0c\u5b83\u5305\u542b\u4e86\u5404\u79cd\u64cd\u4f5c\u6570\u7ec4\u7684\u9759\u6001\u65b9\u6cd5\u3002<code>toString()<\/code>\u65b9\u6cd5\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\uff0c\u80fd\u591f\u5c06\u4e00\u4e2a\u6570\u7ec4\u8f6c\u6362\u6210\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165<code>java.util.Arrays<\/code>\u5305\u3002\u7136\u540e\uff0c\u8c03\u7528<code>Arrays.toString(\u6570\u7ec4\u540d)<\/code>\u65b9\u6cd5\uff0c\u4f20\u5165\u4f60\u60f3\u8981\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u6570\u7ec4\u5143\u7d20\u5c06\u88ab\u9017\u53f7\u548c\u7a7a\u683c\u9694\u5f00\uff0c\u6574\u4e2a\u6570\u7ec4\u5219\u88ab\u65b9\u62ec\u53f7\u5305\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import java.util.Arrays;<\/p>\n<p>public class ArrayToStringDemo {<\/p>\n<p>  public static void m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n(String[] args) {<\/p>\n<p>    int[] numbers = {1, 2, 3, 4, 5};<\/p>\n<p>    String str = Arrays.toString(numbers);<\/p>\n<p>    System.out.println(str);<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528Java 8\u7684<code>Stream API<\/code><\/h3>\n<\/p>\n<p><p>Java 8\u5f15\u5165\u4e86\u6d41<code>Stream API<\/code>\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b0\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6570\u7ec4\u6216\u96c6\u5408\u3002\u901a\u8fc7\u4f7f\u7528<code>Stream API<\/code>\uff0c\u53ef\u4ee5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\uff0c\u8fdb\u800c\u5229\u7528<code>map<\/code>\u5c06\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u4f7f\u7528<code>collect<\/code>\u6536\u96c6\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d41\uff0c\u7136\u540e\u4f7f\u7528<code>map<\/code>\u65b9\u6cd5\u5c06\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u5230\u7ed9\u5b9a\u7684\u51fd\u6570\u4e0a\u3002\u6b64\u5904\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>String::valueOf<\/code>\u5c06\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>Collectors.joining()<\/code>\u6536\u96c6\u5668\u5c06\u6d41\u4e2d\u7684\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import java.util.Arrays;<\/p>\n<p>import java.util.stream.Collectors;<\/p>\n<p>public class StreamApiExample {<\/p>\n<p>  public static void main(String[] args) {<\/p>\n<p>    int[] numbers = {1, 2, 3, 4, 5};<\/p>\n<p>    String str = Arrays.stream(numbers)<\/p>\n<p>                       .mapToObj(String::valueOf)<\/p>\n<p>                       .collect(Collectors.joining(&quot;, &quot;, &quot;[&quot;, &quot;]&quot;));<\/p>\n<p>    System.out.println(str);<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>StringBuilder<\/code><\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u7ec4\uff0c\u6216\u8005\u51fa\u4e8e\u6027\u80fd\u7684\u8003\u8651\uff0c<code>StringBuilder<\/code>\u53ef\u80fd\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002<code>StringBuilder<\/code>\u80fd\u591f\u63d0\u9ad8\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u6548\u7387\uff0c\u51cf\u5c11\u5185\u5b58\u7684\u5206\u914d\u548c\u590d\u5236\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a<code>StringBuilder<\/code>\u5b9e\u4f8b\uff0c\u5e76\u904d\u5386\u6570\u7ec4\uff0c\u5c06\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230<code>StringBuilder<\/code>\u4e2d\u3002\u53ef\u4ee5\u624b\u52a8\u6dfb\u52a0\u5206\u9694\u7b26\u3001\u524d\u7f00\u548c\u540e\u7f00\uff0c\u4ee5\u751f\u6210\u7b26\u5408\u9884\u671f\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public class StringBuilderExample {<\/p>\n<p>  public static void main(String[] args) {<\/p>\n<p>    int[] numbers = {1, 2, 3, 4, 5};<\/p>\n<p>    StringBuilder sb = new StringBuilder(&quot;[&quot;);<\/p>\n<p>    for (int i = 0; i &lt; numbers.length; i++) {<\/p>\n<p>      sb.append(numbers[i]);<\/p>\n<p>      if (i &lt; numbers.length - 1) {<\/p>\n<p>        sb.append(&quot;, &quot;);<\/p>\n<p>      }<\/p>\n<p>    }<\/p>\n<p>    sb.append(&quot;]&quot;);<\/p>\n<p>    System.out.println(sb.toString());<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u91c7\u7528<code>String.join()<\/code><\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5bf9\u8c61\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c<code>String.join()<\/code>\u662f\u4e00\u4e2a\u76f8\u5bf9\u8f83\u65b0\u4e14\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5206\u9694\u7b26\u8fde\u63a5\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u6b64\u65b9\u6cd5\u5728Java 8\u4e2d\u5f15\u5165\uff0c\u7528\u4e8e\u5b57\u7b26\u4e32\u6570\u7ec4\u6216\u4efb\u4f55Iterable\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u4f60\u7684\u6570\u7ec4\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u5982\u679c\u4e0d\u662f\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u6216\u6620\u5c04\u4e3a\u5b57\u7b26\u4e32\u6570\u7ec4\u3002\u4e4b\u540e\uff0c\u53ea\u9700\u8c03\u7528<code>String.join()<\/code>\uff0c\u6307\u5b9a\u5206\u9694\u7b26\u548c\u6570\u7ec4\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public class StringJoinExample {<\/p>\n<p>  public static void main(String[] args) {<\/p>\n<p>    String[] words = {&quot;Hello&quot;, &quot;World&quot;, &quot;!&quot;};<\/p>\n<p>    String str = String.join(&quot; &quot;, words);<\/p>\n<p>    System.out.println(str);<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u548c\u9700\u6c42\uff0c\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u5728\u5904\u7406Java\u9879\u76ee\u4e2d\u7684\u6570\u636e\u8868\u793a\u65f6\uff0c\u5927\u5927\u63d0\u9ad8\u4f60\u7684\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5728Java\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><\/p>\n<p>A: \u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u3002\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Arrays\u7c7b\u7684toString()\u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9toString()\u65b9\u6cd5\u6765\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528StringBuilder\u7c7b\uff0c\u6839\u636e\u9700\u8981\u5c06\u6570\u7ec4\u7684\u5143\u7d20\u8ffd\u52a0\u5230StringBuilder\u5bf9\u8c61\u4e2d\uff0c\u7136\u540e\u4f7f\u7528toString()\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>Q: \u600e\u6837\u5728Java\u9879\u76ee\u4e2d\u5c06\u6570\u7ec4\u5143\u7d20\u4ee5\u7279\u5b9a\u5206\u9694\u7b26\u8fde\u63a5\u6210\u5b57\u7b26\u4e32\uff1f<\/strong><\/p>\n<p>A: \u5982\u679c\u60a8\u60f3\u8981\u4ee5\u7279\u5b9a\u7684\u5206\u9694\u7b26\u8fde\u63a5\u6570\u7ec4\u5143\u7d20\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528StringJoiner\u7c7b\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aStringJoiner\u5bf9\u8c61\uff0c\u5e76\u6307\u5b9a\u6240\u9700\u7684\u5206\u9694\u7b26\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u3002\u7136\u540e\uff0c\u4f7f\u7528add()\u65b9\u6cd5\u5c06\u6570\u7ec4\u5143\u7d20\u6dfb\u52a0\u5230StringJoiner\u5bf9\u8c61\u4e2d\uff0c\u6700\u540e\u4f7f\u7528toString()\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u5728Java\u9879\u76ee\u4e2d\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u53ef\u8bfb\u6027\u826f\u597d\u7684\u5b57\u7b26\u4e32\u8868\u8fbe\u5f62\u5f0f\uff1f<\/strong><\/p>\n<p>A: \u8981\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u53ef\u8bfb\u6027\u826f\u597d\u7684\u5b57\u7b26\u4e32\u8868\u8fbe\u5f62\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Arrays\u7c7b\u7684toString()\u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u5982\u679c\u6570\u7ec4\u5143\u7d20\u662f\u590d\u6742\u5bf9\u8c61\uff0ctoString()\u65b9\u6cd5\u9ed8\u8ba4\u4f1a\u8fd4\u56de\u5b83\u4eec\u7684\u5f15\u7528\u503c\u3002\u4e3a\u4e86\u83b7\u5f97\u66f4\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32\u8868\u8fbe\u5f62\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Arrays\u7c7b\u7684deepToString()\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u4ee5\u5d4c\u5957\u65b9\u5f0f\u6253\u5370\u6570\u7ec4\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u5728Java\u9879\u76ee\u4e2d\u662f\u4e00\u9879\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528Arrays.toS [&hellip;]","protected":false},"author":3,"featured_media":275177,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275153"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=275153"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275153\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/275177"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=275153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=275153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=275153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}