代码之家  ›  专栏  ›  技术社区  ›  Martijn Courteaux

Java:泛型方法和数字

  •  16
  • Martijn Courteaux  · 技术社区  · 15 年前

    List 数不清。

    我尝试的是:

    public static <T extends Number> T sumList(List<T> data)
    {
        T total = 0;
        for (T elem : data)
        {
            total += elem;
        }
        return total;
    }
    

    there is no += operator in T total can't be assigned to zero

    我该怎么做?

    谢谢

    4 回复  |  直到 15 年前
        1
  •  19
  •   Mark Peters    15 年前

    有很多方法可以把它们结合在一起,但老实说,泛型并不是解决问题的方法。为每个具体的原始包装器类型构建一个方法,并分别实现它们。要使它成为通用的,那就太麻烦了;算术运算不能通用地发生。

    把它变成普通的也不会有什么好处。它是如此简单和稳定的代码,您不必担心代码重复,因为它不会改变。而且人们不会将自己的数字类型传递给您的代码;它所应用的类型域已经有了很好的定义和有限性。

        2
  •  4
  •   Mark Peters    13 年前

    lambdaj 代码)注意它需要所有元素都是相同的类型(不能真正添加 Byte 和一个 BigDecimal )如果不是这样的话,你可以扔一个CCE,也不会处理定制的 Number

    public class SumAggregator<T extends Number> {
        public T aggregate(Iterable<T> iterable) {
            T result = null;
            for (T item : iterable) {
                result = aggregate(result, item);
            }
            return result;
        }
    
    
        @SuppressWarnings("unchecked")
        protected T aggregate(T first, T second) {
            if (first == null) {
                return second;
            } else if (second == null) {
                return first;
            } else if (first instanceof BigDecimal) {
                return (T) aggregate((BigDecimal) first, (BigDecimal) second);
            } else if (second instanceof BigInteger) {
                return (T) aggregate((BigInteger) first, (BigInteger) second);
            } else if (first instanceof Byte) {
                return (T) aggregate((Byte) first, (Byte) second);
            } else if (first instanceof Double) {
                return (T) aggregate((Double) first, (Double) second);
            } else if (first instanceof Float) {
                return (T) aggregate((Float) first, (Float) second);
            } else if (first instanceof Integer) {
                return (T) aggregate((Integer) first, (Integer) second);
            } else if (first instanceof Long) {
                return (T) aggregate((Long) first, (Long) second);
            } else if (first instanceof Short) {
                return (T) aggregate((Short) first, (Short) second);
            } else {
                throw new UnsupportedOperationException("SumAggregator only supports official subclasses of Number");
            }
        }
    
        private BigDecimal aggregate(BigDecimal first, BigDecimal second) {
            return first.add(second);
        }
    
        private BigInteger aggregate(BigInteger first, BigInteger second) {
            return first.add(second);
        }
    
        private Byte aggregate(Byte first, Byte second) {
            return (byte) (first + second);
        }
    
        private Double aggregate(Double first, Double second) {
            return first + second;
        }
    
        private Float aggregate(Float first, Float second) {
            return first + second;
        }
    
        private Integer aggregate(Integer first, Integer second) {
            return first + second;
        }
    
        private Long aggregate(Long first, Long second) {
            return first + second;
        }
    
        private Short aggregate(Short first, Short second) {
            return (short) (first + second);
        }
    }
    

    This code executed on ideone with examples

        3
  •  0
  •   Sarmun    15 年前

    可以使用库,例如 Generic Java Math (其数字部分),它为数字子类提供泛型操作。它提供了一种通用的处理数字的方法,就像在您的示例中一样,但是通过传递Arithmetics对象,这就完成了实际的计算。参见主页上的示例。
    它必须使用装箱类型,所以如果您需要速度,在Java中没有通用实现可以做到这一点,那么您必须使用基本类型。

        4
  •  0
  •   Shalin    14 年前

    我相信这就是你想要的。。

    马克·彼得斯的回答也是正确的,但这也是一个解决方案。

    public class GenericSum {
    @SuppressWarnings("unchecked")
    public static <E> E sumArray(E[] inputArray) {
        Double result = 0.0;
        // Sum array elements
        for (E element : inputArray) {
            result = ((Number) result).doubleValue()
                    + ((Number) element).doubleValue();
        }
        return ((E) result);
    }
    
    public static void main(String args[]) {
        // Create arrays of Integer, Double
        Integer[] intArray = { 1, 2, 3, 4, 5 };
        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
    
        System.out.println("Sum of doubles : " + sumArray(intArray));
        System.out.println("Sum of doubles : " + sumArray(doubleArray));
    }
    
    }