public static int sum ( int [ ] x ) { double sum = 0.0 ; for ( int n : x ) { sum += n ; } if ( sum > Integer . MAX_VALUE || sum < - Integer . MAX_VALUE ) { throw new ArithmeticException ( "Sum overflow: " + sum ) ; } return ( int ) sum ; }
Returns the sum of an array .