There are so many ways to solve this! I have usually gone for the usual for loop with an index
and just add the trailing character up until the n-1
position. But today I decided to run a couple of quick tests to see which method is faster, and here are my results:
(Also, I am assuming --believing-- that StrinBuffer .append() has an amortized time of O(1). )
Methods used: For loop, Foreach loop, and Iterator
1) For loop
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)
}
For one thing I just realized, I could have sped this loop up by saving the stringArray.size()-1
call to a variable rather than have it called so many times (my bad!). Aside from that, I'd figure that the worst part of this loop is the stringArray.get(index)
because the rest of the code used pertains to the StringBuffer, which is used in the other loops as well.
Nonetheless, it shouldn't be bad at all because .get() is constant time, O(1).
2) Foreach loop
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) With Iterator
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)
}
Possible time problem? The double questioning of it.hasNext()
. But alas,it should not be much of a problem in terms of performance because if you look at the code involved in hasNext()
for the Iterator returned by ArrayList you'll see it's a mere comparison, so it would be O(1) time.
public boolean hasNext() {
return cursor != size;
}
Conclusion
All of the loops end up having, theoretically, O(n) time complexity. They will vary very slightly depending on the comparisons made, but not by much. That means that you can choose whichever method you would like without worrying on performance, so you have the luxury to choose the code that reads the best or suits you the best. Isn't that great?
Here are a couple of quick tests to back up the theory.
RESULTS
Measured in seconds.
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: I had to put the test results in a "code block" because for some styling reason they are showing in a single line? :S