I'm reviewing a piece of code that looks like this:
public WrappedItem[] processItems(Item[] items) { List<WrappedItem> wrappedItems = new ArrayList<>(); for (Item item : items) { /* …do something with the item… */ wrappedItems.add(WrappedItem.wrap(item)); } return wrappedItems.toArray(new WrappedItem[0]); }
A list is created so that the enhanced for loop can be used and then it is converted to an array to be returned.
Would this be better from a performance and code style perspective?
public WrappedItem[] processItems(Item[] items)
{
WrappedItem[] wrappedItems = WrappedItem[items.length];
for (int i = 0; i < items.length; i++)
{
Item item = items[i]
/* …do something with the item… */
wrappedItems[i] = WrappedItem.wrap(item);
}
return wrappedItems;
}
item
is not wrapped when put inwrappedItems
). This is for Stack Overflow. – rolfl Jul 30 '14 at 15:10