有很多方法可以解決這個問題!我通常會去平常的for loop with an index
,只需將尾隨字符添加到n-1
的位置即可。但是今天我決定進行一些快速測試,看看哪種方法更快,以下是我的結果:
(另外,我假設 - 相信 - StrinBuffer .append()的分期時間爲O(1))
使用
方法:對於循環,foreach循環和Iterator
1)循環
private static String getStringWithForIndex(ArrayList<String> stringArray){
StringBuffer buffer = new StringBuffer(); //O(1)
int index; //O(1)
for(index=0; index<stringArray.size()-1; ++index){ //In all O(n)
buffer.append(stringArray.get(index));
buffer.append("\n");
}
buffer.append(stringArray.get(index)); //O(1);
return buffer.toString(); O(n)
}
對於一兩件事,我才意識到,我可以加速這個循環起來通過將stringArray.size()-1
調用保存到變量而不是調用它很多次(我的壞!)。除此之外,我認爲這個循環最糟糕的部分是stringArray.get(index)
,因爲所用代碼的其餘部分與StringBuffer有關,它也用於其他循環。 ()是不變的,因爲.get()是不變的時間,O(1)。
2)Foreach循環
private static String getStringWithForEach(ArrayList<String> stringArray){
StringBuffer buffer = new StringBuffer(); //O(1)
for(String word : stringArray){ // In all O(n)
buffer.append(word);
buffer.append("\n");
}
buffer.deleteCharAt(buffer.length()-1);
//O(1) because IT'S THE LAST CHARACTER ALWAYS
return buffer.toString(); //O(n)
}
3)同迭代
private static String getStringWithIterator(ArrayList<String> stringArray){
Iterator it = stringArray.iterator(); //O(1)
StringBuffer buffer = new StringBuffer(); //O(1)
while(it.hasNext()){ //In all O(n)
buffer.append(it.next());
if(it.hasNext())
buffer.append("\n");
}
return buffer.toString(); //O(n)
}
可能的時間的問題? it.hasNext()
的雙重提問。但是,可惜的是,在性能方面應該不是什麼大問題,因爲如果你看看ArrayList返回的Iterator的代碼hasNext()
,你會發現它僅僅是一個比較,所以它將是O(1)次。
public boolean hasNext() {
return cursor != size;
}
結論
所有的循環的結束有,理論上,O(n)的時間複雜度。它們會根據比較而略有不同,但不會太多。這意味着您可以選擇任何您喜歡的方式,而無需擔心性能問題,因此您可以選擇最適合您的代碼或最適合您的代碼。這不是很好嗎?
以下是一些快速測試的備份理論。
成績 以秒爲單位。
test1的62.9kb
With Iterator: 0.003
With Foreach: 0.007
With ForIndex: 0.002
With Iterator: 0.008
With Foreach: 0.009
With ForIndex: 0.002
With Iterator: 0.004
With Foreach: 0.015
With ForIndex: 0.002
With Iterator: 0.007
With Foreach: 0.008
With ForIndex: 0.003
With Iterator: 0.003
With Foreach: 0.003
With ForIndex: 0.002
With Iterator: 0.006
With Foreach: 0.01
With ForIndex: 0.002
*Average* With Iterator: 0.006
*Average* With Foreach: 0.009
*Average* With ForIndex: 0.002
test2的6.4 MB
With Iterator: 0.102
With Foreach: 0.115
With ForIndex: 0.121
With Iterator: 0.104
With Foreach: 0.109
With ForIndex: 0.118
With Iterator: 0.106
With Foreach: 0.123
With ForIndex: 0.126
With Iterator: 0.099
With Foreach: 0.109
With ForIndex: 0.12
With Iterator: 0.098
With Foreach: 0.109
With ForIndex: 0.119
With Iterator: 0.1
With Foreach: 0.119
With ForIndex: 0.122
*Average* With Iterator: 0.102
*Average* With Foreach: 0.114
*Average* With ForIndex: 0.121
TEST3 47 MB
With Iterator: 0.116
With Foreach: 0.137
With ForIndex: 0.131
With Iterator: 0.118
With Foreach: 0.146
With ForIndex: 0.119
With Iterator: 0.115
With Foreach: 0.125
With ForIndex: 0.137
With Iterator: 0.11
With Foreach: 0.111
With ForIndex: 0.145
With Iterator: 0.096
With Foreach: 0.108
With ForIndex: 0.113
With Iterator: 0.096
With Foreach: 0.102
With ForIndex: 0.115
*Average* With Iterator: 0.108
*Average* With Foreach: 0.122
*Average* With ForIndex: 0.127
PS:我不得不將測試結果放在一個「代碼塊」中,因爲某些樣式原因它們顯示在一行中? :S
https://code.google.com/p/guava-libraries/wiki/StringsExplained查看此內容。 – Nishant
類似問題:http://stackoverflow.com/questions/599161/best-way-to-convert-an-arraylist-to-a-string和http://stackoverflow.com/questions/1751844/java-convert- liststring-to-a-joind-string – user2314737