Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array declaration trick : Is it bad to use it?

Tags:

java

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?

like image 726
le-doude Avatar asked Aug 14 '13 15:08

le-doude


1 Answers

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 :)

like image 199
rocketboy Avatar answered Sep 20 '22 21:09

rocketboy