Your question is somewhat difficult for Java:
double and float are primitive types, and therefore they are not part of the class hierarchy. The double and float shells extend Number , which extends Object , but- An array of primitive types does not match an array of objects, and Java, for example, does not use autobox a
float[] to float[] . - There is no
isNan(Number n) or isNan(Object o) method in the Java API, but the ones you used that expect a double or float . However, you can do Double.isNan(n.doubleValue()) for any Number n .
TL DR In Java, the usual practice for primitive types is to have separate implementations for each of them, just like you.
EDIT: As @azurefrog suggested:
public static boolean areValuesValid(Number[] values, int numElements) { if (values == null || values.length != numElements) { return false; } for (Number value : values) { if (Double.isNaN(value.doubleValue())) { return false; } } return true; }
And then you have to use Apache Commons ArrayUtils :
public static boolean areValuesValid(double[] values, int numElements) { return areValuesValid(ArrayUtils.toObject(values), numElements); } public static boolean areValuesValid(float[] values, int numElements) { return areValuesValid(ArrayUtils.toObject(values), numElements); }
EDIT2: The @shmosel solution passes the array as an Object , and from here avoids converting the entire array to a box type. A solution worth considering is to avoid this overhead.
source share