I'm writing a simple Java 2D game, displaying a grid. The grid is (for the moment) a simple 2 dimensional array of specific game cells:
int height = 10;
int width = 10;
MyObject[][] table = new MyObject[height][width];
Often, I have to perform a specific method of my object over the full table:
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
table[y][x].MyMethod();
}
}
I have dozens of methods for my cells (computing game rules, etc.), and it gets very tiresome to have to write always the same block of code with just the name of the method (occasionnaly, with a parameter) changing.
Is there some shortcut, or some trick, in Java, to allow an easier and more readable approach:
table.MyMethod();
SomeFactory(table, MyMethod);
I'm willing to change the design of my table, say, to turn it into a class, if possible using generics so as to allow reusability. I'm quite aware such things exist for values (fill method of Arrays), but is there a way to let methods be a dynamic parameter?
Thanks for your help.