I run into this case a lot of times when doing simple text processing and print statements where I am looping over a collection and I want to special case the last element (
I'd go with your second example, ie. handle the special case outside of the loop, just write it a bit more straightforward:
String itemOutput = "["; if (items.length > 0) { itemOutput += items[0]; for (int i = 1; i < items.length; i++) { itemOutput += ", " + items[i]; } } itemOutput += "]";