一、数组作为方法的参数和返回值
public class Test09{
public static void main(String[] args){
int[] arr = {6,43,53,56,467,41,35};
int[] newArr = getMaxAndMin(arr);
System.out.println("最大值为:" + newArr[0]);
System.out.println("最小值为:" + newArr[1]);
}
public static int[] getMaxAndMin(int[] arr){
int max = arr[0];
int min = arr[0];
for(int i = 1;i<arr.length;i++){
if(max < arr[i]){
max = arr[i];
}
if(min > arr[i]){
min = arr[i];
}
}
int[] maxAndMin = {max,min};
return maxAndMin;
}
}
二、可变参数
public class Test10{
public static void main(String[] args){
int max = getMax(1,2,3,4,5,6,7);
System.out.println(max);
}
public static int getMax(int... arr){
if(arr.length == 0){
return -1;
}
int max = arr[0];
for(int i = 1;i<arr.length;i++){
if(max < arr[i]){
max = arr[i];
}
}
return max;
}
public static void method(int i,String... ss){
}
}