How do I clear a String local to a method? My text gets appended everytime the method is run
I have a local String in a method like so:
String jString = new String();
or
String jString = "";
The result is the same.
Later in the method I append:
for(int i = 0; i != someArrayList.size(); ++i) {
jString += someArrayList.get(i).getText() + "\n";
}
Everytime the method is run, the text from getText() gets appended to jString.
Why is this and how can I prevent it from happening? Thanks!
OK edit. This is basically what happens. Either that or you tell me it is impossible to get the results I described with this code.
public void requestData() {
String jString = new String();
for(int i = 0; i != someArrayList.size(); ++i) {
jString += s开发者_如何学运维omeArrayList.get(i).getText() + "\n";
}
}
Say inside the array I have values ["a", "b", "c"]. When I run the method jString contains: a b c Next time I run it, I want it to be the new contents of the array not a b c plus the new contents.
The code you describe does exactly as you require.
public void requestData() { String jString = new String(); for(int i = 0; i != someArrayList.size(); ++i) { jString += someArrayList.get(i).getText() + "\n"; } }
When you call this method, it will create a new (empty) jString
and then append each item in the list to it, separated by a newline character. There is no way that jString
could continue to carry the value from the previous call, since the variable leaves and re-enters scope when the method returns and is called again.
By the way, you should still use a StringBuilder
object.
public void requestData() { StringBuilder jStringBuilder = new StringBuilder(); for (int i = 0; i < someArrayList.size(); i++) { jStringBuilder.append(someArrayList.get(i).getText()).append('\n'); } String jString = jStringBuilder.toString(); }
According to the code which you have written, it will add the text from the array to the jString variable which you cant prevent. But if you don't want that to happen then if you can mention about the logic of your code then according to that we can help you.
EDIT: According to your code it will work as expected by you. So i think the problem is with the array which you are accessing. ie. for the 1st if array contains a,b,c then jstring = "abc"; and for the 2nd time if array contains a,b,c,d,e, then jString="abcde";
This is because jString
variable is not local in the method and the method +=
s it on every call. You should:
- Make it local and return the constructed value, or
- Use
StringBuilder
orStringBuffer
(the best choice not to put burden to garbage collector).
Check where the jString
is being defined.
Based on the results, I am guessing that you have defined it like the first code block here.
public class MyClass { String jString = new String(); public void myMethod() { for(int i = 0; i != someArrayList.size(); ++i) { jString += someArrayList.get(i).getText() + "\n"; } } }
This does not clear the jString
each time the method is called. The jString
is initialized when the class is instantiated. Each call to the method keeps adding on to it.
I suspect that what you really want is something like this:
public class MyClass { public void myMethod() { String jString = new String(); for(int i = 0; i != someArrayList.size(); ++i) { jString += someArrayList.get(i).getText() + "\n"; } } }
If you declare the jString
inside the method, then it will clear each time.
The second example is the one where the string is "local to the method". In the first example, the string is a property of the class.
One other point of importance. When appending strings like this, it is wise to use a StringBuilder
class. This will cause a lot less memory to be used when building the string. There are other questions on SO that explain why, if you are interested.
remove the +
jString = someArrayList.get(i).getText() + "\n";
Will stop appending it. And just replace jString each time, but I doubt if this is what you want either.
精彩评论