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);
}
}
有沒有可能優化上述代碼?代碼基本上用於查找和替換字符串的ArrayList
內的字符串。我們是否有任何提議可以直接在ArrayList
內找到並替換,而不必先取得字符串,然後更換?查找並替換爲字符串ArrayList
對於cource,我們可以將最後3行組合在一起,如下所示。但是這隻會節省我猜測的代碼行數。
arrLst.set(loopIndex2, arrLst.get(loopIndex2).replaceAll(",", "Q"));
如果你想用另一個字符替換一個字符,那麼一個改進可能是使用replace(',','Q')'而不是'replaceAll(「,」,Q「)',這將不必要地涉及到正則表達式引擎。也不需要像loopIndex2這樣命名循環索引,簡單的'i'就足夠了。 「有沒有可能優化上面的代碼?」如果你的代碼有效,那麼你應該在http://codereview.stackexchange.com/上提問這個問題。 – Pshemo 2014-09-13 18:20:23
一個不可變對象的列表不能被更新,除了通過get-change-set範例, – laune 2014-09-13 18:24:09
Laune,所以如果我們使用StringBuilder而不是Strings,我們可以做到內聯?所以方法參數可能需要更改爲ArrayList arrLst。不知道它是否有效。讓我嘗試。 –
2014-09-13 18:33:33