源码
package sto.pdd.util;
public class VariableParameter {
/* 求若干个整型数中的最大值
* 可变参数items
*/
public static int getMax(int... items){
int max = Integer.MIN_VALUE;
for(int item : items){
max = item > max? item : max;
}
return max;
}
public static void main(String[] args) {
// items是一个整型数组
int[] items = { 2, 5, 20, 6, 9 };
System.out.println("最大值:" + getMax(2, 1, 4, 7, 2, -1, 3, 3));
System.out.println("最大值:" + getMax(items));
}
}
本文介绍了一个使用可变参数的方法来寻找整型数组中最大值的Java示例。通过一个公共静态方法 getMax,可以接受任意数量的整型参数,并返回其中的最大值。示例展示了如何调用此方法并打印结果。
115

被折叠的 条评论
为什么被折叠?



