0

I have two functions

public static double avg(int[] values) {
    if(values == null || values.length == 0) return -1;
    double sum = 0;

    for(int value:values) {
        sum = sum + value;
    }
    return (sum/values.length);
}

public static double avg(double[] values) {
    if(values == null || values.length == 0) return -1;
    double sum = 0;

    for(double value:values) {
        sum = sum + value;
    }
    return (sum/values.length);
}

I am using Spel expression engine to evaluate the expressions. When i deploy this code and invoke expression avg({3,4,5}) or avg({3.0,4.0,5.0}), i get the below error,

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1033E:(pos  0): Method call of 'avg' is ambiguous, supported type conversions allow multiple variants to match

Does int[] array implicitly get converted into double[] during evaluation?

Shall i make it single function avg(double[] values)??

Thanks,

Vijay Bhore

Here to Learn.
  • 677
  • 2
  • 10
  • 31

1 Answers1

0

{3,4,5} can be interpreted as array of ints or as array of doubles. use double arr[] = {3,4,5}; avg(arr); or int arr[] = {3,4,5}; avg(arr); and tou should have no errors

EOG
  • 1,677
  • 2
  • 22
  • 36