I see that obviously java does not allow to pass arrays directly to methods (didn't know this before). For example this does not compile:
public static void main(String[] args) {
testFunk({1,2,3,4});
}
public static void testFunk(int[] a) {}
But this does:
public static void main(String[] args) {
int[] a = {1,2,3,4};
testFunk(a);
}
public static void testFunk(int[] a) {}
But what if i need to create an enum with a constructor that accepts an array?
For example I tried the following code, but it does not compile for the same reason as above:
public enum FigureType {
SQUARE({{true, true},{false, false}});
private boolean[][] matrix;
private FigureType(boolean[][] matrix) {
this.matrix = matrix;
}
}
Unfortunately there does not seem to be a simple workaround here. The only option that comes to my mind is to build something like this:
public enum FigureType {
SQUARE(() -> {boolean[][] array = {{true,true},{false,false}}; return array; });
private boolean[][] matrix;
private FigureType(Supplier<boolean[][]> supplier) {
this.matrix = supplier.get();
}
}
But this looks quite ugly ... is there some better solution?
Thanks and greetings, Daniel