如何将结帐 StringBuilder 对象传递给方法?

How to pass a checkout StringBuilder object to a method?

我有一个方法及其要求

private static someMethod initSomething(StringBuilder someString, int number){
   ...  
   someString = new StringBuilder();     <- ????
   this.someString.append(someString);   <- ????
}

initSomething("ddddd", 1234);

我只想在此方法中传递一个字符串。而且我不喜欢使用普通的String,因为它比较慢StringBuilder。

您写道:

private static someMethod initSomething(StringBuilder someString, int number){ 
   someString = new StringBuilder();     <- You just overwrote `someString`
   this.someString.append(someString);   <- Appending `someString`onto itself?
   // You are also missing `return`. And what is `someMethod`?
}

我用观察和问题修改了对原始代码的注释。我想你想做的是这样的:

private static StringBuilder initSomething(String someString, int number){  
   StringBuilder builder = new StringBuilder();
   builder.append(someString);
   // more code here?
   return builder;
}

或者,您可以 return 像这样的字符串:

return bulder.toString();

如果您需要将 StringBuilder 对象传递给该方法,那么您应该这样做:

private static String initSomething(StringBuilder builder, int number){
   builder.append(...);  <- what is being appended, number? You'll need to pass what is being appended to the method
   // more code here?
   return builder.toString();
}

综上所述,你的问题一点都不清楚。我不完全确定你想解决什么。希望这对您有所帮助。