有很多方法可以解决这个问题!我通常会去平常的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