I am using strings for concat operations. I want to use stringbuffer/stringbuilder as they are more effective.
Original code:
List<BDDObject> childlist;
String newassetReferenceType = new String();
for (int i = 0; i < childlist.size(); i++)
{
newassetReferenceType = ((String) childlist.get(i).getValue(IDDConstants.IDD_COMPOUND_ASSET_REF_TYPE_NAME)).toLowerCase().trim().concat(((String) childlist.get(i).getValue(
IDDConstants.IDD_COMPOUND_ASSET_REF_VALUE)).trim());
}
SO I tried to change it as Changed code:
List<BDDObject> childlist;
StringBuffer newassetReferenceType = new StringBuffer();
for (int i = 0; i < childlist.size(); i++)
{
newassetReferenceType = ((String) childlist.get(i).getValue(IDDConstants.IDD_COMPOUND_ASSET_REF_TYPE_NAME)).toLowerCase().trim().append(((String) childlist.get(i).getValue(
IDDConstants.IDD_COMPOUND_ASSET_REF_VALUE)).trim());
}
Now there is an error that trim() and toLowerCase() methods can not be used. But I have to use them anyhow. Can somebody tell me how to get the advantage of trim() and toLowerCase()and still use StringBuffer.
Your solution should work just fine. I think your parentheses are just a little bit off. You should perform trim() and toLowerCase() operations on the strings before appending them to your StringBuffer.
I rewrote your code to illustrate the concept, using proper parenthesization. Feel free to reformat it and put it in one line, but I think this way it is more readable and less prone to mistakes.
List<BDDObject> childlist;
StringBuffer newassetReferenceType = new StringBuffer();
for (int i = 0; i < childlist.size(); i++)
{
var value1 = (String) childlist.get(i).getValue(IDDConstants.IDD_COMPOUND_ASSET_REF_TYPE_NAME);
var value2 = (String) childlist.get(i).getValue(IDDConstants.IDD_COMPOUND_ASSET_REF_VALUE);
newassetReferenceType.Append(value1.toLowerCase().trim())
.append(value2.trim());
}