通过遍历字符列表并使用StringBuilder类创建字符串,可以将字符列表转换为字符串。
演示该程序的程序如下。
import java.util.Arrays;
import java.util.List;
public class Example {
public static void main(String[] args) {
List<Character> cList = Arrays.asList('B', 'e', 'a', 'u', 't', 'y');
StringBuilder sb = new StringBuilder();
for (Character c : cList) {
sb.append(c);
}
String str = sb.toString();
System.out.println("The string obtained is: " + str);
}
}输出结果
The string obtained is: Beauty
现在让我们了解上面的程序。
首先,指定字符列表。然后,使用StringBuilder类通过遍历字符列表并将其附加来创建字符串。最后,显示字符串str。证明这一点的代码片段如下所示-
List<Character> cList = Arrays.asList('B', 'e', 'a', 'u', 't', 'y');
StringBuilder sb = new StringBuilder();
for (Character c : cList) {
sb.append(c);
}
String str = sb.toString();
System.out.println("The string obtained is: " + str);