使用java获取一个int类型数组中的最大值

// 获取一个int类型数组中的最大值
public class MaxArray {

public static void main(String[] args) {
int[] intArray = {1,2,3,4};
int max = getMax(intArray);
System.out.println(max);
}

private static int getMax(int[] intArray) {
int max = intArray[0];//假设数组中的第一个元素的值最大
for(int i=1;i<intArray.length;i++) {//从数组的第二个元素开始遍历
if(max < intArray[i]) {//如果max小于数组中的值时
max = intArray[i];//将数组中的值赋值给max
}

}
return max;
}
}
最后修改:2022 年 01 月 13 日 10 : 50 PM
如果觉得我的文章对你有用,请随意赞赏