如何在Java中更改为StringBuilder

时间:2018-10-25 13:42:37

标签: java stringbuilder

我正在尝试将以下代码更改为StringBuilder,以便可以格式化ArrayList的输出。

 private static String createFormattedList(List<List<String>> failureList) {
    String lineBreak = "</br>";
    String result = "";
    for (List<String> failure : failureList) {

        for (String item : failure) {
            result += item + lineBreak;
        }
        result += lineBreak;

    }

    return result;
}

1 个答案:

答案 0 :(得分:2)

使用以下代码:

private static String createFormattedList(List<List<String>> failureList) {
    String lineBreak = "</br>";
    StringBuilder result = new StringBuilder();
    for (List<String> failure : failureList) {
        for (String item : failure) {
            result.append(item).append(lineBreak);
        }
        result.append(lineBreak);
    }
    return result.toString();
}