简体   繁体   English

Java 不均匀多维数组

[英]Java Uneven Multidimensional array

I came by this piece of code and I'm not sure what is exactly happening.我是通过这段代码来的,我不确定到底发生了什么。 It's as follows:如下:

       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];//

What are the last 4 lines doing?最后 4 行在做什么?

The above code does not compile.上面的代码不能编译。 But I have a strong feeling OP meants something like this:但我有一种强烈的感觉 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];

This creates an array of the following shape (up->down = 1st dimension, left->right= 2nd dimension):这将创建一个具有以下形状的数组(上->下 = 1 维,左->右 = 2 维):

*
**
***
****

More specific, when we query the length of the arrays, we get these results:更具体地说,当我们查询数组的长度时,我们得到以下结果:

twod.length -> 4
twod[0].length -> 1
twod[1].length -> 2
twod[2].length -> 3
twod[3].length -> 4

What you posted didn't compile for me.你发布的内容不适合我。 I think this is what you want for jagged arrays.我认为这就是您想要的锯齿状数组。

   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];//

Results (In debug):结果(调试中):

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM