Can I use generic for float [] and int []?

we have two classes of processes that are almost identical, except that One is used to process float [] data, and the other for int [] data.

I am going to combine them into one. My initial thinking is common to float [] and int [].

Java generic is new to me. So I wrote the following test method to find out how generic works, but I can't get it to work for me.

public <T> T caculate(T[] values)
    {
        T result;
        for(int i = 0; i < values.length; i++)
        {
            result = result + values[i];
        }

        result = result/(values.length);
        return result;       
    }

Can anyone change the above method for me? float and int are primitive types in Java. Probably the above method is wrong.

+3
source share
2 answers

, , , One float[], int[].

, . - ; .. .

Integer[] float[] <T> <T extends Number>. :

  • .

  • .

  • , T extends Number. Number .


, , - , () . , ..... , . .

+3

, + T. , T , . , , , .

int [], float [] , float ints.

+1

All Articles