String[] characters = {"H", "e", "l", "l", "o"};StringBuffer sb = new StringBuffer();for (String c : characters) { sb.append(c);}String result = sb.toString();System.out.println(result);
时间: 2023-07-19 18:19:44 浏览: 56
输出结果为:
```
Hello
```
相关问题
stringbuffer
StringBuffer is a mutable sequence of characters that can be modified without creating a new object. It is a thread-safe class, which means that multiple threads can access the same instance of StringBuffer without any problem.
The StringBuffer class is similar to the String class, but it provides methods to modify the contents of the string. For example, you can append characters to a StringBuffer, insert characters at a specific position, delete characters, and more.
Here is an example of using StringBuffer:
```
StringBuffer sb = new StringBuffer("Hello");
sb.append(" world!");
System.out.println(sb.toString()); // "Hello world!"
```
In this example, we create a StringBuffer with the initial value "Hello". We then use the `append` method to add the string " world!" to the end of the buffer. Finally, we use the `toString` method to convert the StringBuffer to a regular String and print it. The output is "Hello world!".
阅读全文