数组的遍历

遍历一维数组

public class forEach {
    public static void main(String[] args) {
        int[] a = {6,5,4,3,2,1};
        // 遍历数组 for
        for (int i=0; i<a.length; i++)
            if (i == a.length-1)
                System.out.print(a[i]);
            else
                System.out.print(a[i] + ", ");
        System.out.println();

        // 遍历数组  - foreach
        for (int item : a)
            if (item == a[a.length-1])
                System.out.print(item);
            else
                System.out.print(item + ", ");
    }
}

遍历二维数组

        for (int i=0; i<b.length; i++) {
            for (int j=0; j<b[i].length; j++)
                if (j == b[i].length-1)
                    System.out.print(b[i][j]);
                else
                    System.out.print(b[i][j] + ", ");
            System.out.println();
        }

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页