for-each循环

for-each循环又叫for增强循环,不需要下标就可以顺序的便历整个数组例如

package cn.hsop.fentian;

public class Forxunhuan {
    public static void main(String[] args) {

        int[] num = { 5, 8, 3, 6 };//定义数组
        int max=num[0] ;//假设最大值
        //遍历数组
        for (int a : num) {
            System.out.print(a);
        }//遍历数组
            for (int b : num) {
                if (b<max) {
                    max = b;
                    
                }
                
            }
            
        System.out.print(max);
    }

}

这里需要理解的是;int a是一个变量,他的类型要和数组的类型一样。num就是数组的名字。

原文地址:https://www.cnblogs.com/liuxingzhi/p/6748288.html