有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java非均匀多维数组

我是根据这段代码来的,我不确定到底发生了什么。内容如下:

       int twod [] [] = new int[4][];// i know the first one is row

       twod [0]  = new int[1];//
       twod [1]  = new int[2];//  What are these? 
       twod [2]  = new int[3];//
       twod [3]  = new int[4];//

最后4行在做什么


共 (2) 个答案

  1. # 1 楼答案

    上面的代码无法编译。但我有一种强烈的感觉,OP的意思是:

       int twod[][] = new int[4][];// i know the first one is row
    
       twod[0] = new int[1];
       twod[1] = new int[2];
       twod[2] = new int[3];
       twod[3] = new int[4];
    

    这将创建以下形状的数组(向上->;向下=第一维,向左->;向右=第二维):

    *
    **
    ***
    ****
    

    更具体地说,当我们查询数组的长度时,我们会得到以下结果:

    twod.length -> 4
    twod[0].length -> 1
    twod[1].length -> 2
    twod[2].length -> 3
    twod[3].length -> 4
    
  2. # 2 楼答案

    你发布的内容没有为我编译。我想这就是你想要的锯齿阵列

       int twod [][] = new int[4][];// i know the first one is row
    
       twod[0] = new int[1];//
       twod[1] = new int[2];//  What are these? 
       twod[2] = new int[3];//
       twod[3] = new int[4];//
    

    结果(在调试中):

    enter image description here