我只能用字符串来做这个,例如:
String str = "";
for (int i = 0; i < 100; i++) {
str = i + str;
}
有没有办法用StringBuilder实现这个?谢谢。
我只能用字符串来做这个,例如:
String str = "";
for (int i = 0; i < 100; i++) {
str = i + str;
}
StringBuilder sb = new StringBuilder();
for(int i=0;i<100;i++){
sb.insert(0, Integer.toString(i));
}
警告:这样做破坏了StringBuilder
的设计思路,但可以实现你所要求的功能。
更好的技术(虽然仍不理想):
StringBuilder
中。StringBuilder
。这将把一个O(n²)的解决方案变成O(n)。
您可以使用strbuilder.insert(0,i);
strbuilder
是一个变量,而不是类名(确实,StringBuilder#insert
不是一个静态方法,因此你不能像StringBuilder.insert(0,i)
这样调用它)。 - logi-kal也许我理解有误,但你想要得到一个看起来像这样的字符串,"999897969594...543210"
,对吗?
StringBuilder sb = new StringBuilder();
for(int i=99;i>=0;i--){
sb.append(String.valueOf(i));
}
Stack<String> textStack = new Stack<String>();
// push the strings to the stack
while(!isReadingTextDone()) {
String text = readText();
textStack.push(text);
}
// pop the strings and add to the text builder
String builder = new StringBuilder();
while (!textStack.empty()) {
builder.append(textStack.pop());
}
// get the final string
String finalText = builder.toString();
ArrayDeque
而不是Stack
。"更完整和一致的后进先出(LIFO)堆栈操作集由{@link Deque}接口及其实现提供,应优先使用该类而不是此类。" - Luna这个帖子很老,但你也可以考虑使用递归解决方案,并将StringBuilder传递给fill方法。这样可以避免任何反向处理等问题。只需要使用递归来设计迭代,并仔细选择退出条件。
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
doRecursive(sb, 100, 0);
System.out.println(sb.toString());
}
public static void doRecursive(StringBuilder sb, int limit, int index) {
if (index < limit) {
doRecursive(sb, limit, index + 1);
sb.append(Integer.toString(index));
}
}
}
StringBuilder sb = new StringBuilder();
for(int i=0;i<100;i++){
sb.insert(0,i);
}
注意:由于插入方法接受所有类型的原始数据,因此您可以将其用于int、long、char[]等类型。
https://gist.github.com/SidWagz/e41e836dec65ff24f78afdf8669e6420
上面的要点提供了详细的代码,任何人都可以运行。 我在这里使用了几种增加字符串的方法:1)将其附加到 StringBuilder 中,2)像 @Mehrdad 所示一样在 StringBuilder 的前面插入,3)部分地从 StringBuilder 的前面和后面插入,4)使用列表从末尾附加,5)使用双端队列从前面附加。// Case 2
StringBuilder build3 = new StringBuilder();
IntStream.range(0, MAX_STR)
.sequential()
.forEach(i -> {
if (i%2 == 0) build3.append(Integer.toString(i)); else build3.insert(0, Integer.toString(i));
});
String build3Out = build3.toString();
//Case 5
Deque<String> deque = new ArrayDeque<>();
IntStream.range(0, MAX_STR)
.sequential()
.forEach(i -> {
if (i%2 == 0) deque.addLast(Integer.toString(i)); else deque.addFirst(Integer.toString(i));
});
String dequeOut = deque.stream().collect(Collectors.joining(""));
实际上,第2种情况的性能比第1种情况要快得多,这一点我似乎无法理解。我假设在前追加和后追加的情况下,StringBuilder内部缓冲区的增长是相同的。我甚至将最小堆设置为非常大的数量,以避免堆增长延迟,如果这可能会起到作用的话。也许有更好理解的人可以在下面发表评论。
Difference Between String, StringBuilder And StringBuffer Classes
String
String is immutable ( once created can not be changed )object. The object created as a
String is stored in the Constant String Pool.
Every immutable object in Java is thread-safe, which implies String is also thread-safe. String
can not be used by two threads simultaneously.
String once assigned can not be changed.
StringBuffer
StringBuffer is mutable means one can change the value of the object. The object created
through StringBuffer is stored in the heap. StringBuffer has the same methods as the
StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread
safe .
Due to this, it does not allow two threads to simultaneously access the same method. Each
method can be accessed by one thread at a time.
But being thread-safe has disadvantages too as the performance of the StringBuffer hits due
to thread-safe property. Thus StringBuilder is faster than the StringBuffer when calling the
same methods of each class.
String Buffer can be converted to the string by using
toString() method.
StringBuffer demo1 = new StringBuffer("Hello") ;
// The above object stored in heap and its value can be changed.
/
// Above statement is right as it modifies the value which is allowed in the StringBuffer
StringBuilder
StringBuilder is the same as the StringBuffer, that is it stores the object in heap and it can also
be modified. The main difference between the StringBuffer and StringBuilder is
that StringBuilder is also not thread-safe.
StringBuilder is fast as it is not thread-safe.
/
// The above object is stored in the heap and its value can be modified
/
// Above statement is right as it modifies the value which is allowed in the StringBuilder
这样怎么样:
StringBuilder builder = new StringBuilder();
for(int i=99;i>=0;i--){
builder.append(Integer.toString(i));
}
builder.toString();
或者
StringBuilder builder = new StringBuilder();
for(int i=0;i<100;i++){
builder.insert(0, Integer.toString(i));
}
builder.toString();
但是这样做会使操作的时间复杂度从O(N)变为O(N^2)。
Java文档中的片段:
将Object参数的字符串表示形式插入到此字符序列中。总体效果就像第二个参数通过方法
String.valueOf(Object)
转换为字符串,然后该字符串的字符被插入到指定偏移量处的字符序列中一样。
AbstractStringBuilder
移动插入索引之后的所有内容以为插入内容腾出空间。然而,这只是一种实现细节,而不是原则性问题。 - entonio