要找到数组中的最大值,可以使用以下方法:
public static int findMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static int findMax(int[] arr) {
return Arrays.stream(arr).max().getAsInt();
}
以上两种方法都可以找到数组中的最大值,具体使用哪种方法取决于个人偏好和项目需求。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Java怎么获取数组中的最大值