多维数组的定义

158 阅读1分钟

多维数组可以看成以数组为元素的数组。可以有二维,三维甚至更多维数组,但是实际开发中用的非常少,最多到二维数组(学习容器后,我们一般使用容器,二维数组用的都很少)

例:

package com.gaobo.testshuzu;
//测试多维数组都初始化

public class Test08 {
    public static void main(String[] args) {
        //java中多维数组都声明和初始化应按从低维到高维到顺序进行
        int [] [] a = new int[3][];
        a [0] = new int[2];
        a [1] = new int[4];
        a [2] = new int[3];
        //int a1[][] = new int [][4];非法
        a[0][0] = 100;
        a[0][1] = 200;
    }
}

二维数组的静态初始化

例:

package com.gaobo.testshuzu;
//测试多维数组都初始化

public class Test08 {
    public static void main(String[] args) {
        //java中多维数组都声明和初始化应按从低维到高维到顺序进行
        int [] [] a = new int[3][];
        a [0] = new int[2];
        a [1] = new int[4];
        a [2] = new int[3];
        //int a1[][] = new int [][4];非法
        a[0][0] = 100;
        a[0][1] = 200;


        int[] [] b = {{1,2,3,},
                     {3,4},
                     {3,4,6,7}
        };
        System.out.println(b[2] [3]);
    }
}

二维数组的动态初始化

例:

//import java.util.Arracys

int [] [] c = new int[3][];
//c [0] ={1,2,5}//错误,没有声明类型就初始化
c[0] = new int[]{1,2};
c[1] = new int[]{2,2};
c[2] = new int[]{2,2,3,4};
System.out.println(c[2][3]);
System.out.println(Arrays.toString(c[0]));
System.out.println(Arrays.toString(c[1]));
System.out.println(Arrays.toString(c[2]));