简体   繁体   English

Java通用运算符

[英]Java generic operators

I have the following method: 我有以下方法:

  private <E extends Number> double GetAverage(ArrayList<E> al)
  {
    double total = 0;
    Iterator<E> itr = al.iterator();
    while(itr.hasNext())
    {
      total += itr.next();
    }

    return total;
  }

but it does not compile. 但它没有编译。 I get a 我得到了

"total cannot be resolved or is not a field" “总不能解决或不是一个领域”

on line 在线

"total += itr.next();" “total + = itr.next();”

I understand that Java doesn't know the value of E, but I hope you understand what I mean, what is the best way to create a generic method that adds the total(Numeric values) of an ArrayList. 我知道Java不知道E的价值,但我希望你理解我的意思,创建一个添加ArrayList的总数(数值)的通用方法的最佳方法是什么。

Your code looks like this: 您的代码如下所示:

itr.next().
total += itr.next();

Which the compiler is reading as this: 编译器正在读取的内容如下:

itr.next().total += itr.next();

The compiler is suggesting that there is no field named total that is accessible on Number . 编译器建议没有可在Number上访问的名为total字段。 You probably meant to not have this line: 你可能意味着没有这条线:

itr.next().

On another note, I am not sure that Number will auto-unbox into a double . 另一方面,我不确定Number会自动拆箱double You may need to call Number#doubleValue() on the Number instance. 您可能需要在Number实例上调用Number#doubleValue()

total += itr.next().doubleValue();

There's actually syntax errors with your code, you have line that's not complete. 您的代码实际上存在语法错误,您的行不完整。 Your full code should be: 您的完整代码应该是:

private <E extends Number> double GetAverage(ArrayList<E> al) {
    double total = 0;
    Iterator<E> itr = al.iterator();
    while (itr.hasNext()) {
        E next = itr.next();
        total += next.doubleValue();
    }

    return total;
}

Note: the return of 'total' and not undefined retVal 注意:返回'total'而不是undefined retVal

You can't add a generic Number to a double . 您不能将通用Number添加到double There's no way to store BigInteger or BigDecimal inside a double field, so addition is just not definable on ( doubleNumber ). 有没有办法来存储BigIntegerBigDecimal一个内部的double场,所以除了是不上(可定义doubleNumber )。

Try 尝试

total += itr.next().doubleValue();

if you can safely assume that the numbers are reducible to double without too much loss of precision. 如果你可以安全地假设这些数字可以减少到double而没有太多的精度损失。

To make your code properly generic, try this: 要使代码正确通用,请尝试以下操作:

private <E extends Number> double GetAverage(Iterable<E> list)
{
  double total = 0;
  for (E num : list) {
    total += num.doubleValue();
  }
  return total;
}

If you want to treat null as a NaN value instead of failing with an exception you will have to do that with an if inside the loop. 如果要将null视为NaN值而不是因异常而失败,则必须在循环内部使用if

You have an extraneous line in the code - the first itr.next() line (that ends with a dot). 代码中有一条无关的行 - 第一行itr.next()行(以点结尾)。 Also, you can't add a Number, you need to add the doubleValue. 此外,您无法添加数字,您需要添加doubleValue。 Finally, you need to return total, not retVal. 最后,您需要返回总数,而不是retVal。

eg 例如

 private <E extends Number> double GetAverage(ArrayList<E> al)
   {
     double total = 0;
     Iterator<E> itr = al.iterator();
     while(itr.hasNext())
     {
       total += itr.next().doubleValue();
     }

     return total;
   }

Since all Numbers have getDouble, you could simplify things by using the new for() iterator. 由于所有Numbers都有getDouble,因此可以使用new for()迭代器简化操作。

private <E extends Number> double GetAverage(Collection<E> al)
   {
     double total = 0;
     for (Number n : al)
       total += n.doubleValue();

     return total;
   }

Depending upon details, you might be able to get rid of the E extends Number stuff and just say Number... 根据细节,您可能可以摆脱E扩展数字的东西,只说数字......

And, definitely change the input parameter from ArrayList<E> to List<E> or even Collection<E> , as I did in the second example. 并且,确实将输入参数从ArrayList<E>更改为List<E>甚至Collection<E> ,就像我在第二个示例中所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM