{"id":37182,"date":"2024-11-26T08:38:11","date_gmt":"2024-11-26T00:38:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37182\/"},"modified":"2024-11-26T08:38:11","modified_gmt":"2024-11-26T00:38:11","slug":"java%e6%80%8e%e4%b9%88byte%e6%95%b0%e7%bb%84%e8%bf%bd%e5%8a%a0%e7%a9%ba%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37182\/","title":{"rendered":"Java\u600e\u4e48byte\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c"},"content":{"rendered":"<blockquote><p>\n  \u7b54\u6848\uff1a java \u4e2d\u53ef\u901a\u8fc7 arrays.copyof \u548c stringbuilder \u65b9\u6cd5\u5411\u5b57\u8282\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c\u3002arrays.copyof\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5c06\u7a7a\u683c\u6dfb\u52a0\u5230\u6307\u5b9a\u4f4d\u7f6e\u3002stringbuilder\uff1a\u5c06\u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6dfb\u52a0\u7a7a\u683c\u540e\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111302214699876.jpg\" class=\"aligncenter\" title=\"Java\u600e\u4e48byte\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c\u63d2\u56fe\" alt=\"Java\u600e\u4e48byte\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u5411\u5b57\u8282\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5411\u5b57\u8282\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c\u3002\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 Arrays.copyOf \u65b9\u6cd5\uff1a<\/p>\n<pre>byte[] bytesWithSpaces = Arrays.copyOf(originalBytes, originalBytes.length + numOfSpaces);\n\nfor (int i = originalBytes.length; i &lt; bytesWithSpaces.length; i++) {\n    bytesWithSpaces[i] = ' '; \/\/ \u6dfb\u52a0\u7a7a\u683c\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0coriginalBytes \u662f\u539f\u59cb\u5b57\u8282\u6570\u7ec4\uff0cnumOfSpaces \u662f\u8981\u6dfb\u52a0\u7684\u7a7a\u683c\u6570\u3002Arrays.copyOf \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u5177\u6709\u539f\u59cb\u5b57\u8282\u6570\u7ec4\u7684\u957f\u5ea6\u52a0\u4e0a\u6307\u5b9a\u7684\u7a7a\u95f4\u6570\u3002\u63a5\u7740\uff0c\u4f7f\u7528\u5faa\u73af\u5c06\u7a7a\u683c\u6dfb\u52a0\u5230\u65b0\u6570\u7ec4\u4e2d\u3002<\/p>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 StringBuilder \u7c7b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>StringBuilder sb = new StringBuilder();\nsb.append(originalBytes);\n\nfor (int i = 0; i &lt; numOfSpaces; i++) {\n    sb.append(' ');\n}\n\nbyte[] bytesWithSpaces = sb.toString().getBytes();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u91cc\uff0cStringBuilder \u5bf9\u8c61\u7528\u4e8e\u521b\u5efa\u5b57\u7b26\u4e32\u8868\u793a\u7684\u5b57\u8282\u6570\u7ec4\u3002\u7136\u540e\uff0c\u4f7f\u7528\u5faa\u73af\u5411\u5b57\u7b26\u4e32\u4e2d\u6dfb\u52a0\u7a7a\u683c\u3002\u6700\u540e\uff0c\u4f7f\u7528 getBytes() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<p>\u6839\u636e\u5177\u4f53\u60c5\u51b5\uff0c\u9009\u62e9\u4f7f\u7528 Arrays.copyOf \u6216 StringBuilder \u65b9\u6cd5\u66f4\u4e3a\u5408\u9002\u3002\u5982\u679c\u60a8\u9700\u8981\u4e00\u4e2a\u65b0\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u5219 Arrays.copyOf \u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u60a8\u5bf9\u5b57\u7b26\u4e32\u8868\u793a\u66f4\u611f\u5174\u8da3\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 StringBuilder\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava\u600e\u4e48byte\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c\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>\u7b54\u6848\uff1a java \u4e2d\u53ef\u901a\u8fc7 arrays.copyof \u548c stringbuilder \u65b9\u6cd5\u5411\u5b57\u8282\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c\u3002arrays.copyof\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5c06\u7a7a\u683c\u6dfb\u52a0\u5230\u6307\u5b9a\u4f4d\u7f6e\u3002stringbuilder\uff1a\u5c06\u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6dfb\u52a0\u7a7a\u683c\u540e\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u3002 Java \u4e2d\u5411\u5b57\u8282\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5411\u5b57\u8282\u6570\u7ec4\u8ffd\u52a0\u7a7a\u683c\u3002\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 Arrays.copyOf \u65b9\u6cd5\uff1a byte[] bytesWithSpaces = Arrays.copyOf(originalBytes, originalBytes.length + numOfSpaces); for (int i = originalBytes.length; i &lt; bytesWithSpaces.length; i++) { bytesWithSpaces[i] = &#8216; &#8216;; \/\/ \u6dfb\u52a0\u7a7a\u683c } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0coriginalBytes \u662f\u539f\u59cb\u5b57\u8282\u6570\u7ec4\uff0cnumOfSpaces \u662f\u8981\u6dfb\u52a0\u7684\u7a7a\u683c\u6570\u3002Arrays.copyOf \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u8be5\u6570\u7ec4\u5177\u6709\u539f\u59cb\u5b57\u8282\u6570\u7ec4\u7684\u957f\u5ea6\u52a0\u4e0a\u6307\u5b9a\u7684\u7a7a\u95f4\u6570\u3002\u63a5\u7740\uff0c\u4f7f\u7528\u5faa\u73af\u5c06\u7a7a\u683c\u6dfb\u52a0\u5230\u65b0\u6570\u7ec4\u4e2d\u3002 \u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 StringBuilder \u7c7b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b StringBuilder sb = new StringBuilder(); sb.append(originalBytes); for (int [&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-37182","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37182","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=37182"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37182\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}