数组遍历

数组遍历

通过for:each()遍历数组

一元数组

     double[] a;//定义
        a=new double[5];//创建动态数组
        Scanner scanner=new Scanner(System.in);
        for(int i=0;i<a.length;i++)
            a[i]=scanner.nextDouble();
        for(double element:a)
            System.out.print(element+" ");
        System.out.println();

二元数组

int[][] arr2 = {{1, 2, 3}, {4, 5, 26}, {7, 8, 9}} ;//静态初始化

        for(int[] row : arr2)
        {
            for(int element : row)
            {
                System.out.println(element);
            }
        }

 这是使用for:each语句简单遍历数组的方法

原文地址:https://www.cnblogs.com/huangui/p/12657041.html