public static int preaparePatternForJFugue(ArrayList <String> arrLst) {
String contents;
for(int loopIndex2=0;loopIndex2<arrLst.size();loopIndex2++) {
contents=arrLst.get(loopIndex2);
contents=contents.replaceAll(",", "Q");
arrLst.set(loopIndex2, contents);
}
}
Is there any possibility of optimizing the above code? Code is basically for finding and replacing string within the ArrayList of strings. Do we have any provsion to find and replace within ArrayList directly without getting the strings first and then replacing?
Of cource, we can combine last 3 lines together as below. But this will only save number of code lines I guess.
arrLst.set(loopIndex2, arrLst.get(loopIndex2).replaceAll(",", "Q"));
replace(',', 'Q')instead ofreplaceAll(",", "Q")which will unnecessarily involve regex engine. Also there is no need to name loop index likeloopIndex2, simpleiis enough. "Is there any possibility of optimizing the above code?" if your code works then you should be asking this question on codereview.stackexchange.com.StringBuilderdoes not have a replace method, so even if you make the list generic aStringBuilderyou'll still need to convert it toString.