3 回答
TA贡献1851条经验 获得超3个赞
Java 8 或更高版本:
final String result = wrongLetters.stream().map(String::valueOf).collect(Collectors.joining());
System.out.println("Number of errors: " + wrongLetters.size() + " (" + result + ")");
TA贡献1818条经验 获得超8个赞
从字符的数组列表中创建一个字符串
String str = "";
for (Character c : wrongLetters) {
str += c;
}
然后 :
System.out.println("Number of errors: " + wrongLetters.size() + " (" + String.join("", str) + ")");
或制作一个字符串生成器:
StringBuilder stringBuilder= new StringBuilder(wrongLetters.size());
for (Character c : wrongLetters) {
stringBuilder.append(c);
}
然后:
System.out.println("Number of errors: " + wrongLetters.size() + " (" + String.join("", stringBuilder.toString()) + ")");
TA贡献1802条经验 获得超5个赞
愚蠢的答案(只需删除非字母数字):
System.out.println("Number of errors: " + wrongLetters.size() + " (" + String.join("", String.valueOf(wrongLetters).replaceAll("[^A-Za-z0-9]", ""))+ ")");
Java 8+ 的另一个:
StringJoiner joiner = new StringJoiner("");
wrongLetters.stream().forEach(err -> joiner.add(String.valueOf(err)));
System.out.println("Number of errors: " + wrongLetters.size() + " (" + joiner + ")");
没有 Java 8(使用 StringBuilder):
StringBuilder sb = new StringBuilder(" (");
for (Character character : wrongLetters) {
sb.append(character);
}
sb.append(")");
System.out.println("Number of errors: " + wrongLetters.size() + sb.toString());
添加回答
举报