user4199244 wrote:What's wrong with using reverse? It's a perfectly reasonable method call.
The reason for doing it this way is that i'm traversing through another arraylist backwards while doing some calculations on that and adding the results to the new arraylist. The result is ofc that the list is in reverse order. I was hoping there was a simple solution to this other than calling reverse(), but i guess that will have to do.
user4199244 wrote:And was your implementation of reverse() actually faster than the version built into the API? Or did you just assume that, even though you weren't familiar with the class, that whatever you threw together would naturally be faster than that?
thanks. I wanted to avoid reverse() if it was possible simply to avoid overhead. When running though these calculations/iterations a couple of hundred million times, every ms counts :)
user4199244 wrote:That's a bad assumption. A List is not an array and it should not be treated like one. Instead, re-read the documentation of List, to clarify this aspect.
of course not, i just assumed that if it was possible to add an element to the list in the same manner you can to an array
, that i wouldn't need to reverse the entire list, which, without having done any research, i intuitively assume to be somewhat intensive."somewhat intensive" is a highly subjective term. And intuition is a pretty bad thing to apply to performance optimizatons, because it's terribly easy to get it wrong.
I may of course be wrong, but it just feels like i have to do twice the work when reversing rather than just putting the element in the correct index in the first place. But by all means, please feel free to enlighten me.Of course it may be more work, but it might also be irrelevant. If the actual work you do is in any way significant, then reversing the array is very unlikely to take a significant percentage of the total runtime.