数组的遍历

package day04;

public class ArrayTraverse {
    /*数组的遍历:通过循环获取数组中的所有元素,动态获取数组元素个数:数组名.length*/
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        //打印数组的长度,格式:数组名.length
        System.out.println("数组长度:" + arr.length);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

执行结果:

eg:打印最大值

 1 package day04;
 2 
 3 public class ArrayTest01 {
 4     public static void main(String[] args) {
 5         int[] arr = {12, 35, 2, 9, 1, 0, 15};
 6         int max = arr[0];
 7         for (int i = 1; i < arr.length; i++) {
 8             if (arr[i] > max) {
 9                 max = arr[i];
10             }
11         }
12         System.out.println("最大值为:" + max);
13     }
14 }

执行结果:

欢迎批评指正,提出问题,谢谢!
原文地址:https://www.cnblogs.com/xxeleanor/p/14213251.html