老师,我想使用泛型来控制随机生成数组的类型
来源:2-2 使用模板(泛型)编写算法
相信光变成光
2018-10-13
我是这么写的,但是程序会报错
写回答
1回答
-
liuyubobobo
2018-10-14
Math.random() * (rangeR - rangeL + 1) + rangeL 返回的是double,double可以强转成Object没问题,但是不能强转成Integer。你的obj传的肯定不是Double.class:)
实际上,对于这个函数,在Java中是不建议写成泛型函数的。Java的泛型本身就没有那么灵活。所以即使在标准库中很多地方也不使用泛型。尤其是只限定数字类型时。(对于你的这个例子,就是限定数字类型。因为根据你的函数语义,并不是生成随机的可比较元素的数组。比如字符串是可比较的,但字符串无法在int rangeL到int rangeR之间。)
比如Math.abs,取绝对值,Java标准库是怎么实现的呢?答案是这样的:根本不用泛型!:)
/** * Returns the absolute value of an {@code int} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned. * * <p>Note that if the argument is equal to the value of * {@link Integer#MIN_VALUE}, the most negative representable * {@code int} value, the result is that same value, which is * negative. * * @param a the argument whose absolute value is to be determined * @return the absolute value of the argument. */ public static int abs(int a) { return (a < 0) ? -a : a; } /** * Returns the absolute value of a {@code long} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned. * * <p>Note that if the argument is equal to the value of * {@link Long#MIN_VALUE}, the most negative representable * {@code long} value, the result is that same value, which * is negative. * * @param a the argument whose absolute value is to be determined * @return the absolute value of the argument. */ public static long abs(long a) { return (a < 0) ? -a : a; } /** * Returns the absolute value of a {@code float} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned. * Special cases: * <ul><li>If the argument is positive zero or negative zero, the * result is positive zero. * <li>If the argument is infinite, the result is positive infinity. * <li>If the argument is NaN, the result is NaN.</ul> * In other words, the result is the same as the value of the expression: * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))} * * @param a the argument whose absolute value is to be determined * @return the absolute value of the argument. */ public static float abs(float a) { return (a <= 0.0F) ? 0.0F - a : a; } /** * Returns the absolute value of a {@code double} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned. * Special cases: * <ul><li>If the argument is positive zero or negative zero, the result * is positive zero. * <li>If the argument is infinite, the result is positive infinity. * <li>If the argument is NaN, the result is NaN.</ul> * In other words, the result is the same as the value of the expression: * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)} * * @param a the argument whose absolute value is to be determined * @return the absolute value of the argument. */ public static double abs(double a) { return (a <= 0.0D) ? 0.0D - a : a; }
00
相似问题