Say there is a method Operation,
public ObjectOut Operation(ObjectIn input)
What is the best way to apply the operation method to an ObjectIn[] object?
Do you need to explicitely overload Operation,
public ObjectOut[] Operation(ObjectIn[] input) {
ObjectOut[] output = new ObjectOut[input.length];
for (int i=0; i<input.length; i++)
output[i] = Operation(input[i]);
}
Or is there a more generic way of doing this once for all such “scalar” methods?
You could use an approach like this:
Here you define the behaviour of the scalar from outside and apply the behaviour to each array item. But how the items are iterated is not relevant to the caller. (Tell don’t ask)
That way you don’t duplicate the code of the loop that iterates over the items. And you are flexible enough to add various types of scalar methods.
Edit: I added a second generic parameter for the result like Fabian suggested. This makes the method more flexible. I also removed the casting of the array (which was wrong in the first place).