Is there a way to determine if the loop is iterating for the last time. My code looks something like this:
int[] array = {1, 2, 3...};
StringBuilder builder = new StringBuilder();
for(int i : array)
{
builder.append("" + i);
if(!lastiteration)
builder.append(",");
}
Now the thing is I don't want to append the comma in the last iteration. Now is there a way to determine if it is the last iteration or am I stuck with the for loop or using an external counter to keep track.
Here's a SSCCE benchmark I ran (related to what I had to implement) with these results:
On my example at least, skipping the check at every iteration isn't noticeably faster especially for sane volumes of data, but it is faster.