I’m using an enhanced for loop, and it looks something like this:
public void addItems (Iterable<Item> items) {
for (Item item : items) {
addItem(item);
}
}
public void addItems(Item[] items) {
for(Item item : items) {
addItem(item);
}
}
Using the Iterable allows me to use List et al., but I have to have a separate method for regular arrays. Is there some way to combine this (without using reflection, or increasing line count, or creating garbage), so I don’t have to duplicate code?
You could overload the method (Preferred)
or convert to a List each time you call the method.
The second method is less than optimal. As Sticky has mentioned: