那些年可以封神的代码:2,一维数组和二维数组的极简入门

        //方式1:
        //定义长度为6的数组。arr.length为6
        int arr[] = new int[6];
        //索引 0 - 5,一共6个值,分别赋值
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        //System.out.println(Arrays.toString(arr));打印数组
        //System.out.println(arr[5]);//5索引的值为5

        //方式1的另一种方式
        int[] arr11;
        arr11 = new int[5];

        //方式2:
        int[] arr2 = {0, 1, 2, 3, 4, 5};


        //多维数组:1
        //有2行,3列。数组的长度为2
        //1	2 3 这是arr[0]的值
        //4	5 6 这是arr[1]的值
        int arrDual[][] = new int[2][3];

        int t = 0;
        for (int i = 0; i < arrDual.length; i++) {
            for (int j = 0; j < arrDual[i].length; j++) {
                arrDual[i][j] = ++t;
            }
        }
        //数组的长度为2
        System.out.println("数组的长度为:" + arrDual.length);
        for (int i = 0; i < arrDual.length; i++) {
            for (int j = 0; j < arrDual[i].length; j++) {
                System.out.print(arrDual[i][j] + "\t");
            }
            System.out.println();
        }


        System.out.println("==================================");

        //多为数组2:
        //int arrDual2[][] = new int[3][2];
        //有3行,2列。数组的长度为3。
        //arr[0] 1,2
        //arr[1] 3,4
        //arr[2] 5,6
        int arrDual2[][] = {{1, 2}, {3, 4}, {5, 6}};
        //arrDual2的长度为3
        System.out.println("数组的长度为:" + arrDual2.length);

        for (int i = 0; i < arrDual2.length; i++) {
            for (int j = 0; j < arrDual2[i].length; j++) {
                System.out.print(arrDual2[i][j] + "\t");
            }
            System.out.println();
        }

你可能感兴趣的:(java,SE,java,数组,二维数组)