Java function for arrays int [], float [] or double []

I want to write a Java function that will be introduced as input, and int[], float[]or double[]. The algorithm is exactly the same (some kind of scalar product). How to write one function that can handle all types of numeric arrays?

+5
source share
7 answers

You can write one method to do all this, however it will not be readable anywhere. You must choose between what is a common or effective solution.

public static void main(String... args) throws IOException {
    int[] nums = new int[10*1000 * 1000];
    {
        long start = System.nanoTime();
        product2(nums);
        long time = System.nanoTime() - start;
        System.out.printf("Took %.3f seconds to take the product of %,d ints using an int[].%n", time / 1e9, nums.length);
    }
    {
        long start = System.nanoTime();
        product(nums);
        long time = System.nanoTime() - start;
        System.out.printf("Took %.3f seconds to take the product of %,d ints using reflections.%n", time / 1e9, nums.length);
    }
}

public static double product(Object array) {
    double product = 1;
    for (int i = 0, n = Array.getLength(array); i < n; i++)
        product *= ((Number) Array.get(array, i)).doubleValue();
    return product;
}

public static double product2(int... nums) {
    double product = 1;
    for (int i = 0, n = nums.length; i < n; i++)
        product *= nums[i];
    return product;
}

prints

Took 0.016 seconds to take the product of 10,000,000 ints using an int[].
Took 0.849 seconds to take the product of 10,000,000 ints using reflections.

If you only work with relatively small arrays, a general, but less efficient solution can be quite fast.

+2

Java . :

  • Wrapper (Integer[], Float[], Double[]) Number[] . , Java:

public static void main(String[] args) {
    f(new Integer[]{1,2,3});
    f(new Float[]{1,2,3});
    f(new Double[]{1,2,3});
}

private static void f(Number[] numbers) {
    f[0].doubleValue();
}

, .


  • int[] Float[] Double[] . , , int[] Float[], Double[].

  • , Scala , Java Scala.

+6

Java :

  • ... , , , , .

int[] float[] double[] Object, . , , , int, float double .

, , ; Integer[]/float[]/double[] Number[].

+4

:

1) , int [], float [], double [] .

2) Object [] int/float/double. ( )

+1

java.lang.Number Object.

+1
class ArrayMath {
    private ArrayMath() {
    }

    public static <T extends Number> double ScalarProduct(T[] a, T[] b){
        double sum = 0;

        for(int i=0;i<a.length;++i){
            sum += a[i].doubleValue()*b[i].doubleValue();
        }
        return sum;
    }
}
class Sample {
    public static void main(String arg[]){
        Integer[] v1 = { 1, -10, 3, 9, 7, 99, -25 };
        Integer[] v2 = { 1, -10, 3, 9, 7, 99, -25 };
        double p_int = ArrayMath.ScalarProduct(v1, v2);
        Double[] v1_d = { 1.1, -10.5, 3.7, 9.98, 7.4, 9.9, -2.5 };
        Double[] v2_d = { 1.1, -10.5, 3.7, 9.98, 7.4, 9.9, -2.5 };
        Double p_double = ArrayMath.ScalarProduct(v1_d, v2_d);

        System.out.println("p_int:" + p_int);
        System.out.println("p_double:" + p_double);
    }
}
+1

, . , , , !

public class MultiDataType {

    public static void main(String[] args) {

        int[] i = new int[2];
        float[] f = new float[2];
        double[] d = new double[2];
        String str = new String();

        handlingFunction(i);
        handlingFunction(f);
        handlingFunction(d);
        handlingFunction(str);
    }

    public static void handlingFunction(Object o) {
        String classType = null;
        if (o.getClass().getCanonicalName().equals("int[]")) {
            classType = "int[]";// Your handling code goes here
        } else if (o.getClass().getCanonicalName().equals("float[]")) {
            classType = "float[]";// Your handling code goes here
        } else if (o.getClass().getCanonicalName().equals("double[]")) {
            classType = "double[]";// Your handling code goes here
        }else classType = o.getClass().getCanonicalName();

        System.out.println("Object belongs to " + classType);
    }

}

OUTPUT

Object belongs to int[]
Object belongs to float[]
Object belongs to double[]
Object belongs to java.lang.String
0

All Articles