I am declaring this class, that doesn't to be useful.
public class ArrayTrick {
public static char[] arr(char... arr) {
return arr;
}
public static float[] arr(float... arr) {
return arr;
}
public static double[] arr(double... arr) {
return arr;
}
public static long[] arr(long... arr) {
return arr;
}
public static int[] arr(int... arr) {
return arr;
}
public static short[] arr(short... arr) {
return arr;
}
public static byte[] arr(byte... arr) {
return arr;
}
public static boolean[] arr(boolean... arr) {
return arr;
}
public static <T> T[] arr(T... arr) {
return arr;
}
}
which allows me (once I have a static import in my code) to declare arrays like this:
int[][] arr = arr(
arr(1, 2, 1),
arr(2, 1, 3),
arr(3, 3, 3));
personally I find it useful and the few people I work with understand it.
It comes from the fact that I got frustrated by java array declaration after working in python, and I sometime work with keyboards where the curly brackets are hard to find (Italian standard on my old laptop).
What I want to know is : Is there anything bad about working like that? Is it readable enough in your opinion? How come this trick is not famous?
Not much different from
int[][] arr = {{1, 2, 1},
{2, 1, 3},
{3, 3, 3}};
Also, I don't think you can run away from curly brackets in java :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With