编写一个方法getMax求int数组中最大的值
以下是Java语言的实现:
public static int getMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
该方法首先初始化max为数组的第一个元素,然后遍历数组中的每一个元素,如果当前元素比max大,则更新max的值。最后返回max即为数组中最大的值。
原文地址: http://www.cveoy.top/t/topic/fHWF 著作权归作者所有。请勿转载和采集!