<<
Math.min(a, Math.min(b,c));
>>
<<
import org.apache.commons.lang.math.NumberUtils;NumberUtils.min(a,b,c);
NumberUtils.min(new int[]{a,b,c});
>>
Math.min(a, Math.min(b,c));
>>
<<
import org.apache.commons.lang.math.NumberUtils;NumberUtils.min(a,b,c);
NumberUtils.min(new int[]{a,b,c});
>>
return (a > b ? b : a) > c ? c : (a > b ? b : a);
}
xiaohaiZ:同意应该尽量使用类库中的方法,但是如果要在4个数中求最小值你怎办?你写个出来瞧一瞧
kenny1979:不是算法问题
cold_blooded:只有你自已看得懂,0分
@see NumberUtils#min(int[])
cold_blooded的方法虽然你看不懂,不代表只有他自己看得懂三,对不?有兴趣的话看看org.apache.commons.lang.math.NumberUtils是怎么做的吧。
//--------------------------------------------------------------------
/**
* <p>Returns the minimum value in an array.</p>
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws IllegalArgumentException if <code>array</code> is <code>null</code>
* @throws IllegalArgumentException if <code>array</code> is empty
*/
public static long min(long[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns min
long min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}这个是org.apache.commons.lang.math.NumberUtils源码哈。
// developed by the Apache Software Foundation (http://www.apache.org/).
另外,被别人爱也是好事哦。^^