遍历数组

//用方法调用 数组中的最大值

public class MethodTest02 {
    public static void main(String[] args) {
        int[] arr = {11,22,54,23,1,99,5,77};
        int number = getMax(arr);
        System.out.println("number: " + number);
    }
    public static int getMax(int[] arr){
        int max = arr[0];
        for(int x = 1;x < arr.length;x++){
            if(arr[x] > max){
                max = arr[x];
            }
        }
        return max;
    }
}
//用方法的形式 遍历打印一个数组:
public class MethodTest01 {
    public static void main(String[] args) {
        int arr[] = {11,22,33,44,55,88};

        printArray(arr);
    }

    public static void printArray(int[] arr){
        System.out.print("[");
        for(int x=0;x<arr.length;x++){
            if(x == arr.length-1){              //遍历到最后一个元素的写法
                System.out.print(arr[x]);
            }else{
                System.out.print(arr[x] + "," );
            }

        }
        System.out.println("]");
    }
} 

  


//遍历数组最大值: public class ArryMax { public static void main(String[] args) { int[] arr = {12,45,98,73,60}; int max = arr[0]; for(int i=1; i<arr.length; i++){ if(arr[i] > max){ max = arr[i]; } } System.out.println("max:" + max); } } public class ArryMin { public static void main(String[] args) { int[] arr = {12,45,98,73,60}; int min = arr[0]; for(int i=1; i<arr.length; i++){ if(arr[i] < min){ min = arr[i]; } } System.out.println("min:" + min); } } //遍历数组 public class ArrDemo02 { public static void main(String[] args) { int[] arr = {11,22,33,44,55,66}; for(int i=0; i<arr.length; i++){ System.out.println(arr[i]); } } }

 

posted @ 2021-06-18 18:02  walkersss  阅读(24)  评论(0编辑  收藏  举报