{"id":680,"date":"2023-11-29T18:01:01","date_gmt":"2023-11-29T10:01:01","guid":{"rendered":""},"modified":"2023-11-29T18:01:01","modified_gmt":"2023-11-29T10:01:01","slug":"\u7f9e\uff0cJava \u5b57\u7b26\u4e32\u62fc\u63a5\u7adf\u7136\u6709\u8fd9\u4e48\u591a\u59ff\u52bf","status":"publish","type":"post","link":"https:\/\/mushiming.com\/680.html","title":{"rendered":"\u7f9e\uff0cJava \u5b57\u7b26\u4e32\u62fc\u63a5\u7adf\u7136\u6709\u8fd9\u4e48\u591a\u59ff\u52bf"},"content":{"rendered":"
\n

\u4e8c\u54e5\uff0c\u6211\u4eca\u5e74\u5927\u4e8c\uff0c\u770b\u4f60\u5206\u4eab\u7684\u300a\u963f\u91cc\u5df4\u5df4 Java \u5f00\u53d1\u624b\u518c\u300b\u4e0a\u6709\u4e00\u6bb5\u5185\u5bb9\u8bf4\uff1a\u201c\u5faa\u73af\u4f53\u5185\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32\u6700\u597d\u4f7f\u7528 StringBuilder \u7684 append \u65b9\u6cd5\uff0c\u800c\u4e0d\u662f + \u53f7\u64cd\u4f5c\u7b26\u3002\u201d\u5230\u5e95\u4e3a\u4ec0\u4e48\u554a\uff0c\u6211\u5e73\u5e38\u4e00\u76f4\u5c31\u7528\u7684\u2018+\u2019\u53f7\u64cd\u4f5c\u7b26\u554a\uff01\u4e8c\u54e5\u6709\u7a7a\u7684\u65f6\u5019\u80fd\u5426\u5199\u4e00\u7bc7\u6587\u7ae0\u5206\u6790\u4e00\u4e0b\u5462\uff1f<\/p>\n<\/blockquote>\n

\u5c31\u5728\u6628\u5929\uff0c\u4e00\u4f4d\u53eb\u5c0f\u83dc\u7684\u8bfb\u8005\u5fae\u4fe1\u6211\u8bf4\u4e86\u4e0a\u9762\u8fd9\u6bb5\u8bdd\u3002<\/p>\n

\u6211\u5f53\u65f6\u770b\u5230\u8fd9\u6761\u5fae\u4fe1\u7684\u7b2c\u4e00\u611f\u89c9\u662f\uff1a\u5c0f\u83dc\u4f60\u4e5f\u592a\u83dc\u4e86\u5427\uff0c\u8fd9\u90fd\u4e0d\u77e5\u9053\u4e3a\u5565\u554a\uff01\u6211\u4f30\u8ba1\u6b63\u5728\u8bfb\u8fd9\u7bc7\u6587\u7ae0\u7684\u4f60\u4e5f\u4f1a\u6709\u540c\u6837\u7684\u611f\u89c9\u3002<\/p>\n

\u4f46\u626a\u5fc3\u81ea\u95ee\uff0c\u5728\u6211\u505a\u7a0b\u5e8f\u5458\u7684\u524d\u4e24\u5e74\u5185\uff0c\u6211\u4e5f\u4e0d\u77e5\u9053\u4e3a\u5565\u3002\u9047\u5230\u5b57\u7b26\u4e32\u62fc\u63a5\u5c31\u4e0a\u201c+\u201d\u53f7\u64cd\u4f5c\u7b26\uff0c\u752d\u7ba1\u662f\u4e0d\u662f\u5728\u5faa\u73af\u4f53\u5185\u3002\u548c\u5c0f\u83dc\u6bd4\u8d77\u6765\uff0c\u6211\u5f53\u65f6\u53ef\u6ca1\u4ed6\u8fd9\u4e48\u5e78\u8fd0\uff0c\u8fd8\u6709\u4e00\u4f4d\u70ed\u5fc3\u7684\u201c\u4e8c\u54e5\u201d\u80fd\u591f\u5206\u4eab\u8fd9\u4efd\u4ef7\u503c\u8fde\u57ce\u7684\u5f00\u53d1\u624b\u518c\u3002<\/p>\n

\u65e2\u7136\u6211\u8fd9\u4e48\u70ed\u5fc3\u5206\u4eab\uff0c\u4e0d\u5982\u597d\u4eba\u505a\u5230\u5e95\uff0c\u5bf9\u4e0d\u5bf9\uff1f\u6211\u5c31\u8ba4\u8ba4\u771f\u771f\u5730\u5199\u4e00\u7bc7\u6587\u7ae0\uff0c\u7ed9\u5c0f\u83dc\u89e3\u60d1\u4e00\u4e0b\u3002<\/p>\n

01\u3001\u201c+\u201d\u53f7\u64cd\u4f5c\u7b26<\/h3>\n

\u8981\u8bf4\u59ff\u52bf\uff0c\u201c+\u201d\u53f7\u64cd\u4f5c\u7b26\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\u62fc\u63a5\u6700\u5e38\u7528\u7684\u4e00\u79cd\u4e86\uff0c\u6ca1\u6709\u4e4b\u4e00\u3002<\/p>\n

String chenmo = \"\u6c89\u9ed8\"<\/span>;\nString wanger = \"\u738b\u4e8c\"<\/span>;\n\nSystem.out.println(chenmo + wanger);\n<\/code><\/pre>\n

\u6211\u4eec\u628a\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528 JAD \u53cd\u7f16\u8bd1\u4e00\u4e0b\u3002<\/p>\n

String chenmo = \"\\u6C89\\u9ED8\"<\/span>; \/\/ \u6c89\u9ed8<\/span>\nString wanger = \"\\u738B\\u4E8C\"<\/span>; \/\/ \u738b\u4e8c<\/span>\nSystem.out.println((new<\/span> StringBuilder(String.valueOf(chenmo))).append(wanger).toString());\n<\/code><\/pre>\n

\u6211\u53bb\uff0c\u539f\u6765\u7f16\u8bd1\u7684\u65f6\u5019\u628a\u201c+\u201d\u53f7\u64cd\u4f5c\u7b26\u66ff\u6362\u6210\u4e86 StringBuilder \u7684 append \u65b9\u6cd5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u201c+\u201d\u53f7\u64cd\u4f5c\u7b26\u5728\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u65f6\u5019\u53ea\u662f\u4e00\u79cd\u5f62\u5f0f\u4e3b\u4e49\uff0c\u8ba9\u5f00\u53d1\u8005\u4f7f\u7528\u8d77\u6765\u6bd4\u8f83\u7b80\u4fbf\uff0c\u4ee3\u7801\u770b\u8d77\u6765\u6bd4\u8f83\u7b80\u6d01\uff0c\u8bfb\u8d77\u6765\u6bd4\u8f83\u987a\u7545\u3002\u7b97\u662f Java \u7684\u4e00\u79cd\u8bed\u6cd5\u7cd6\u5427\u3002<\/p>\n

02\u3001StringBuilder<\/h3>\n

\u9664\u53bb\u201c+\u201d\u53f7\u64cd\u4f5c\u7b26\uff0cStringBuilder \u7684 append \u65b9\u6cd5\u5c31\u662f\u7b2c\u4e8c\u4e2a\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u59ff\u52bf\u4e86\u3002<\/p>\n

\u5148\u6765\u770b\u4e00\u4e0b StringBuilder \u7c7b\u7684 append \u65b9\u6cd5\u7684\u6e90\u7801\uff1a<\/p>\n

public<\/span> StringBuilder append<\/span>(String str)<\/span> <\/span>{\n    super<\/span>.append(str);\n    return<\/span> this<\/span>;\n}\n<\/code><\/pre>\n

\u8fd9 3 \u884c\u4ee3\u7801\u6ca1\u5565\u53ef\u770b\u7684\uff0c\u53ef\u770b\u7684\u662f\u7236\u7c7b AbstractStringBuilder \u7684 append \u65b9\u6cd5\uff1a<\/p>\n

public<\/span> AbstractStringBuilder append<\/span>(String str)<\/span> <\/span>{\n    if<\/span> (str == null<\/span>)\n        return<\/span> appendNull();\n    int<\/span> len = str.length();\n    ensureCapacityInternal(count + len);\n    str.getChars(0<\/span>, len, value, count);\n    count += len;\n    return<\/span> this<\/span>;\n}\n<\/code><\/pre>\n

1\uff09\u5224\u65ad\u62fc\u63a5\u7684\u5b57\u7b26\u4e32\u662f\u4e0d\u662f null\uff0c\u5982\u679c\u662f\uff0c\u5f53\u505a\u5b57\u7b26\u4e32\u201cnull\u201d\u6765\u5904\u7406\u3002appendNull<\/code> \u65b9\u6cd5\u7684\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n

private<\/span> AbstractStringBuilder appendNull<\/span>()<\/span> <\/span>{\n    int<\/span> c = count;\n    ensureCapacityInternal(c + 4<\/span>);\n    final<\/span> char<\/span>[] value = this<\/span>.value;\n    value[c++] = 'n'<\/span>;\n    value[c++] = 'u'<\/span>;\n    value[c++] = 'l'<\/span>;\n    value[c++] = 'l'<\/span>;\n    count = c;\n    return<\/span> this<\/span>;\n}\n<\/code><\/pre>\n

2\uff09\u62fc\u63a5\u540e\u7684\u5b57\u7b26\u6570\u7ec4\u957f\u5ea6\u662f\u5426\u8d85\u8fc7\u5f53\u524d\u503c\uff0c\u5982\u679c\u8d85\u8fc7\uff0c\u8fdb\u884c\u6269\u5bb9\u5e76\u590d\u5236\u3002ensureCapacityInternal<\/code> \u65b9\u6cd5\u7684\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n

private<\/span> void<\/span> ensureCapacityInternal<\/span>(int<\/span> minimumCapacity)<\/span> <\/span>{\n    \/\/ overflow-conscious code<\/span>\n    if<\/span> (minimumCapacity - value.length > 0<\/span>) {\n        value = Arrays.copyOf(value,\n                newCapacity(minimumCapacity));\n    }\n}\n<\/code><\/pre>\n

3\uff09\u5c06\u62fc\u63a5\u7684\u5b57\u7b26\u4e32 str \u590d\u5236\u5230\u76ee\u6807\u6570\u7ec4 value \u4e2d\u3002<\/p>\n

str.getChars(0<\/span>, len, value, count)\n<\/code><\/pre>\n

03\u3001StringBuffer<\/h3>\n

\u5148\u6709 StringBuffer \u540e\u6709 StringBuilder\uff0c\u4e24\u8005\u5c31\u50cf\u662f\u5b6a\u751f\u53cc\u80de\u80ce\uff0c\u8be5\u6709\u7684\u90fd\u6709\uff0c\u53ea\u4e0d\u8fc7\u5927\u54e5 StringBuffer \u56e0\u4e3a\u591a\u547c\u5438\u4e24\u53e3\u65b0\u9c9c\u7a7a\u6c14\uff0c\u6240\u4ee5\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n

public<\/span> synchronized<\/span> StringBuffer append<\/span>(String str)<\/span> <\/span>{\n    toStringCache = null<\/span>;\n    super<\/span>.append(str);\n    return<\/span> this<\/span>;\n}\n<\/code><\/pre>\n

StringBuffer \u7c7b\u7684 append \u65b9\u6cd5\u6bd4 StringBuilder \u591a\u4e86\u4e00\u4e2a\u5173\u952e\u5b57 synchronized\uff0c\u53ef\u6682\u65f6\u5ffd\u7565 toStringCache = null<\/code>\u3002<\/p>\n

synchronized \u662f Java \u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u5bb9\u6613\u8138\u719f\u7684\u5173\u952e\u5b57\uff0c\u662f\u4e00\u79cd\u540c\u6b65\u9501\u3002\u5b83\u4fee\u9970\u7684\u65b9\u6cd5\u88ab\u79f0\u4e3a\u540c\u6b65\u65b9\u6cd5\uff0c\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n

04\u3001String \u7c7b\u7684 concat \u65b9\u6cd5<\/h3>\n

\u5355\u5c31\u59ff\u52bf\u4e0a\u6765\u770b\uff0cString \u7c7b\u7684 concat \u65b9\u6cd5\u5c31\u597d\u50cf StringBuilder \u7c7b\u7684 append\u3002<\/p>\n

String chenmo = \"\u6c89\u9ed8\"<\/span>;\nString wanger = \"\u738b\u4e8c\"<\/span>;\n\nSystem.out.println(chenmo.concat(wanger));\n<\/code><\/pre>\n

\u6587\u7ae0\u5199\u5230\u8fd9\u7684\u65f6\u5019\uff0c\u6211\u7a81\u7136\u4ea7\u751f\u4e86\u4e00\u4e2a\u5947\u5999\u7684\u60f3\u6cd5\u3002\u5047\u5982\u6709\u8fd9\u6837\u4e24\u884c\u4ee3\u7801\uff1a<\/p>\n

chenmo += wanger\nchenmo = chenmo.concat(wanger)\n<\/code><\/pre>\n

\u5b83\u4eec\u4e4b\u95f4\u7a76\u7adf\u6709\u591a\u5927\u7684\u5dee\u522b\u5462\uff1f<\/p>\n

\u4e4b\u524d\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u5230\uff0cchenmo += wanger<\/code> \u5b9e\u9645\u4e0a\u76f8\u5f53\u4e8e (new StringBuilder(String.valueOf(chenmo))).append(wanger).toString()<\/code>\u3002<\/p>\n

\u8981\u63a2\u7a76\u201c+\u201d\u53f7\u64cd\u4f5c\u7b26\u548c concat<\/code> \u4e4b\u95f4\u7684\u5dee\u522b\uff0c\u5b9e\u9645\u4e0a\u8981\u770b append \u65b9\u6cd5\u548c concat \u65b9\u6cd5\u4e4b\u95f4\u7684\u5dee\u522b\u3002<\/p>\n

append \u65b9\u6cd5\u7684\u6e90\u7801\u4e4b\u524d\u5206\u6790\u8fc7\u4e86\u3002\u6211\u4eec\u5c31\u6765\u770b\u4e00\u4e0b concat \u65b9\u6cd5\u7684\u6e90\u7801\u5427\u3002<\/p>\n

public<\/span> String concat<\/span>(String str)<\/span> <\/span>{\n    int<\/span> otherLen = str.length();\n    if<\/span> (otherLen == 0<\/span>) {\n        return<\/span> this<\/span>;\n    }\n    int<\/span> len = value.length;\n    char<\/span> buf[] = Arrays.copyOf(value, len + otherLen);\n    str.getChars(buf, len);\n    return<\/span> new<\/span> String(buf, true<\/span>);\n}\n<\/code><\/pre>\n

1\uff09\u5982\u679c\u62fc\u63a5\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e3a 0\uff0c\u90a3\u4e48\u8fd4\u56de\u62fc\u63a5\u524d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n

if<\/span> (otherLen == 0<\/span>) {\n    return<\/span> this<\/span>;\n}\n<\/code><\/pre>\n

2\uff09\u5c06\u539f\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6570\u7ec4 value \u590d\u5236\u5230\u53d8\u91cf buf \u6570\u7ec4\u4e2d\u3002<\/p>\n

char<\/span> buf[] = Arrays.copyOf(value, len + otherLen);\n<\/code><\/pre>\n

3\uff09\u628a\u62fc\u63a5\u7684\u5b57\u7b26\u4e32 str \u590d\u5236\u5230\u5b57\u7b26\u6570\u7ec4 buf \u4e2d\uff0c\u5e76\u8fd4\u56de\u65b0\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002<\/p>\n

str.getChars(buf, len);\nreturn<\/span> new<\/span> String(buf, true<\/span>);\n<\/code><\/pre>\n

\u901a\u8fc7\u6e90\u7801\u5206\u6790\u6211\u4eec\u5927\u81f4\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a<\/p>\n

1\uff09\u5982\u679c\u62fc\u63a5\u7684\u5b57\u7b26\u4e32\u662f null\uff0cconcat \u65f6\u5019\u5c31\u4f1a\u629b\u51fa NullPointerException\uff0c\u201c+\u201d\u53f7\u64cd\u4f5c\u7b26\u4f1a\u5f53\u505a\u662f\u201cnull\u201d\u5b57\u7b26\u4e32\u6765\u5904\u7406\u3002<\/p>\n

2\uff09\u5982\u679c\u62fc\u63a5\u7684\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff08\"\"\uff09\uff0c\u90a3\u4e48 concat \u7684\u6548\u7387\u8981\u66f4\u9ad8\u4e00\u70b9\u3002\u6bd5\u7adf\u4e0d\u9700\u8981 new StringBuilder<\/code> \u5bf9\u8c61\u3002<\/p>\n

3\uff09\u5982\u679c\u62fc\u63a5\u7684\u5b57\u7b26\u4e32\u975e\u5e38\u591a\uff0cconcat \u7684\u6548\u7387\u5c31\u4f1a\u4e0b\u964d\uff0c\u56e0\u4e3a\u521b\u5efa\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u8d8a\u591a\uff0c\u5f00\u9500\u5c31\u8d8a\u5927\u3002<\/p>\n

\u6ce8\u610f\u4e86\uff01\uff01\uff01<\/strong><\/p>\n

\u5f31\u5f31\u5730\u95ee\u4e00\u4e0b\u554a\uff0c\u8fd8\u6709\u5728\u7528 JSP \u7684\u540c\u5b66\u5417\uff1fEL \u8868\u8fbe\u5f0f\u4e2d\u662f\u4e0d\u5141\u8bb8\u4f7f\u7528\u201c+\u201d\u64cd\u4f5c\u7b26\u6765\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\uff0c\u8fd9\u65f6\u5019\u5c31\u53ea\u80fd\u7528 concat<\/code> \u4e86\u3002<\/p>\n

${chenmo.concat('-'<\/span>).concat(wanger)}\n<\/code><\/pre>\n

05\u3001String \u7c7b\u7684 join \u65b9\u6cd5<\/h3>\n

JDK 1.8 \u63d0\u4f9b\u4e86\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u59ff\u52bf\uff1aString \u7c7b\u589e\u52a0\u4e86\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5 join\u3002<\/p>\n

String chenmo = \"\u6c89\u9ed8\"<\/span>;\nString wanger = \"\u738b\u4e8c\"<\/span>;\nString cmower = String.join(\"\"<\/span>, chenmo, wanger);\nSystem.out.println(cmower);\n<\/code><\/pre>\n

\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u5b57\u7b26\u4e32\u8fde\u63a5\u7b26\uff0c\u6bd4\u5982\u8bf4\uff1a<\/p>\n

String message = String.join(\"-\"<\/span>, \"\u738b\u4e8c\"<\/span>, \"\u592a\u7279\u4e48\"<\/span>, \"\u6709\u8da3\u4e86\"<\/span>);\n<\/code><\/pre>\n

\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a\u738b\u4e8c-\u592a\u7279\u4e48-\u6709\u8da3\u4e86<\/p>\n

\u6211\u4eec\u6765\u770b\u4e00\u4e0b join \u65b9\u6cd5\u7684\u6e90\u7801\uff1a<\/p>\n

public<\/span> static<\/span> String join<\/span>(CharSequence delimiter, CharSequence... elements)<\/span> <\/span>{\n    Objects.requireNonNull(delimiter);\n    Objects.requireNonNull(elements);\n    \/\/ Number of elements not likely worth Arrays.stream overhead.<\/span>\n    StringJoiner joiner = new<\/span> StringJoiner(delimiter);\n    for<\/span> (CharSequence cs: elements) {\n        joiner.add(cs);\n    }\n    return<\/span> joiner.toString();\n}\n<\/code><\/pre>\n

\u53d1\u73b0\u4e86\u4e00\u4e2a\u65b0\u7c7b StringJoiner\uff0c\u7c7b\u540d\u770b\u8d77\u6765\u5f88 6\uff0c\u8bfb\u8d77\u6765\u4e5f\u5f88\u987a\u53e3\u3002StringJoiner \u662f java.util<\/code> \u5305\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u6784\u9020\u4e00\u4e2a\u7531\u5206\u9694\u7b26\u91cd\u65b0\u8fde\u63a5\u7684\u5b57\u7b26\u5e8f\u5217\u3002\u9650\u4e8e\u7bc7\u5e45\uff0c\u672c\u6587\u5c31\u4e0d\u518d\u505a\u8fc7\u591a\u4ecb\u7ecd\u4e86\uff0c\u611f\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u53bb\u4e86\u89e3\u4e00\u4e0b\u3002<\/p>\n

06\u3001StringUtils.join<\/h3>\n

\u5b9e\u6218\u9879\u76ee\u5f53\u4e2d\uff0c\u6211\u4eec\u5904\u7406\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u7ecf\u5e38\u4f1a\u7528\u5230\u8fd9\u4e2a\u7c7b\u2014\u2014org.apache.commons.lang3.StringUtils<\/code>\uff0c\u8be5\u7c7b\u7684 join \u65b9\u6cd5\u662f\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u4e00\u79cd\u65b0\u59ff\u52bf\u3002<\/p>\n

String chenmo = \"\u6c89\u9ed8\"<\/span>;\nString wanger = \"\u738b\u4e8c\"<\/span>;\n\nStringUtils.join(chenmo, wanger);\n<\/code><\/pre>\n

\u8be5\u65b9\u6cd5\u66f4\u5584\u4e8e\u62fc\u63a5\u6570\u7ec4\u4e2d\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u4e0d\u7528\u62c5\u5fc3 NullPointerException\u3002<\/p>\n

StringUtils.join(null<\/span>)            = null<\/span>\nStringUtils.join([])              = \"\"<\/span>\nStringUtils.join([null<\/span>])          = \"\"<\/span>\nStringUtils.join([\"a\"<\/span>, \"b\"<\/span>, \"c\"<\/span>]) = \"abc\"<\/span>\nStringUtils.join([null<\/span>, \"\"<\/span>, \"a\"<\/span>]) = \"a\"<\/span>\n<\/code><\/pre>\n

\u901a\u8fc7\u67e5\u770b\u6e90\u7801\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\uff0c\u5176\u5185\u90e8\u4f7f\u7528\u7684\u4ecd\u7136\u662f StringBuilder\u3002<\/p>\n

public<\/span> static<\/span> String join<\/span>(final<\/span> Object[] array, String separator, final<\/span> int<\/span> startIndex, final<\/span> int<\/span> endIndex)<\/span> <\/span>{\n    if<\/span> (array == null<\/span>) {\n        return<\/span> null<\/span>;\n    }\n    if<\/span> (separator == null<\/span>) {\n        separator = EMPTY;\n    }\n\n    final<\/span> StringBuilder buf = new<\/span> StringBuilder(noOfItems * 16<\/span>);\n\n    for<\/span> (int<\/span> i = startIndex; i < endIndex; i++) {\n        if<\/span> (i > startIndex) {\n            buf.append(separator);\n        }\n        if<\/span> (array[i] != null<\/span>) {\n            buf.append(array[i]);\n        }\n    }\n    return<\/span> buf.toString();\n}\n<\/code><\/pre>\n

\u5927\u5bb6\u8bfb\u5230\u8fd9\uff0c\u4e0d\u7ea6\u800c\u540c\u4f1a\u6709\u8fd9\u6837\u4e00\u79cd\u611f\u89c9\uff1a\u6211\u9760\uff08\u97f3\u8981\u62d6\u957f\uff09\uff0c\u6ca1\u60f3\u5230\u554a\u6ca1\u60f3\u5230\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5\u8db3\u8db3\u6709 6 \u79cd\u59ff\u52bf\u554a\uff0c\u665a\u4e0a\u56de\u5230\u5bb6\u4e00\u5b9a\u8981\u4e00\u4e00\u5c1d\u8bd5\u4e0b\u3002<\/p>\n

07\u3001\u7ed9\u5c0f\u83dc\u4e00\u4e2a\u7b54\u590d<\/h3>\n

\u6211\u76f8\u4fe1\uff0c\u5c0f\u83dc\u8bfb\u5230\u6211\u8fd9\u7bc7\u6587\u7ae0\u7684\u65f6\u5019\uff0c\u4ed6\u4e00\u5b9a\u4f1a\u660e\u767d\u4e3a\u4ec0\u4e48\u963f\u91cc\u5df4\u5df4\u4e0d\u5efa\u8bae\u5728 for \u5faa\u73af\u4e2d\u4f7f\u7528\u201d+\u201d\u53f7\u64cd\u4f5c\u7b26\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u4e86\u3002<\/p>\n

\u6765\u770b\u4e24\u6bb5\u4ee3\u7801\u3002<\/p>\n

\u7b2c\u4e00\u6bb5\uff0cfor \u5faa\u73af\u4e2d\u4f7f\u7528\u201d+\u201d\u53f7\u64cd\u4f5c\u7b26\u3002<\/p>\n

String result = \"\"<\/span>;\nfor<\/span> (int<\/span> i = 0<\/span>; i < 100000<\/span>; i++) {\n    result += \"\u516d\u516d\u516d\"<\/span>;\n}\n<\/code><\/pre>\n

\u7b2c\u4e8c\u6bb5\uff0cfor \u5faa\u73af\u4e2d\u4f7f\u7528 append\u3002<\/p>\n

StringBuilder sb = new<\/span> StringBuilder();\nfor<\/span> (int<\/span> i = 0<\/span>; i < 100000<\/span>; i++) {\n    sb.append(\"\u516d\u516d\u516d\"<\/span>);\n}\n<\/code><\/pre>\n

\u8fd9\u4e24\u6bb5\u4ee3\u7801\u5206\u522b\u4f1a\u8017\u65f6\u591a\u957f\u65f6\u95f4\u5462\uff1f\u5728\u6211\u7684 iMac \u4e0a\u6d4b\u8bd5\u51fa\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n

1\uff09\u7b2c\u4e00\u6bb5\u4ee3\u7801\u6267\u884c\u5b8c\u7684\u65f6\u95f4\u4e3a 6212 \u6beb\u79d2<\/p>\n

2\uff09\u7b2c\u4e8c\u6bb5\u4ee3\u7801\u6267\u884c\u5b8c\u7684\u65f6\u95f4\u4e3a 1 \u6beb\u79d2<\/p>\n

\u5dee\u8ddd\u4e5f\u592a\u7279\u4e48\u5927\u4e86\u5427\uff01\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n

\u6211\u76f8\u4fe1\u6709\u4e0d\u5c11\u540c\u5b66\u5df2\u7ecf\u6709\u4e86\u81ea\u5df1\u7684\u7b54\u6848\uff1a\u7b2c\u4e00\u6bb5\u7684 for \u5faa\u73af\u4e2d\u521b\u5efa\u4e86\u5927\u91cf\u7684 StringBuilder \u5bf9\u8c61\uff0c\u800c\u7b2c\u4e8c\u6bb5\u4ee3\u7801\u81f3\u59cb\u81f3\u7ec8\u53ea\u6709\u4e00\u4e2a StringBuilder \u5bf9\u8c61\u3002<\/p>\n

08\u3001\u6700\u540e<\/h3>\n

\u8c22\u8c22\u5927\u5bb6\u7684\u9605\u8bfb\uff0c\u539f\u521b\u4e0d\u6613\uff0c\u559c\u6b22\u5c31\u968f\u624b\u70b9\u4e2a\u8d5e\ud83d\udc4d\uff0c\u8fd9\u5c06\u662f\u6211\u6700\u5f3a\u7684\u5199\u4f5c\u52a8\u529b\u3002\u5982\u679c\u89c9\u5f97\u6587\u7ae0\u5bf9\u4f60\u6709\u70b9\u5e2e\u52a9\uff0c\u8fd8\u633a\u6709\u8da3\uff0c\u5c31\u5173\u6ce8\u4e00\u4e0b\u6211\u7684\u516c\u4f17\u53f7\u300c\u6c89\u9ed8\u738b\u4e8c<\/strong>\u300d\uff1b\u56de\u590d\u300c666<\/strong>\u300d\u66f4\u6709 500G \u9ad8\u8d28\u91cf\u6559\u5b66\u89c6\u9891\u76f8\u9001\uff08\u5df2\u5206\u95e8\u522b\u7c7b\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7f9e\uff0cJava \u5b57\u7b26\u4e32\u62fc\u63a5\u7adf\u7136\u6709\u8fd9\u4e48\u591a\u59ff\u52bf\u5c31\u5728\u6628\u5929\uff0c\u4e00\u4f4d\u53eb\u5c0f\u83dc\u7684\u8bfb\u8005\u5fae\u4fe1\u6211\u8bf4\u4e86\u4e0a\u9762\u8fd9\u6bb5\u8bdd\u3002 \u6211\u5f53\u65f6\u770b\u5230\u8fd9\u6761\u5fae\u4fe1\u7684\u7b2c\u4e00\u611f\u89c9\u662f\uff1a...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/680"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=680"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/680\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}