public static double median ( Array array ) throws ExpressionException { int len = array . size ( ) ; if ( len == 0 ) return 0 ; if ( array . getDimension ( ) > 1 ) throw new ExpressionException ( "Median() can only be calculated for one dimensional arrays" ) ; double [ ] arr = new double [ len ] ; for ( int i = 0 ; i < len ; i ++ ) arr [ i ] = _toDoubleValue ( array , i + 1 ) ; Arrays . sort ( arr ) ; double result = arr [ len / 2 ] ; if ( len % 2 == 0 ) { return ( result + arr [ ( len - 2 ) / 2 ] ) / 2 ; } return result ; }
median value of all items in the arrays only works when all values are numeric