简体   繁体   English

是否可以将数组作为数组的元素?

[英]Is it possible to have an array as the elements of an array?

Is it possible to have an array as the elements of an array? 是否可以将数组作为数组的元素? For example in the first index it can have an array of size 3, and the second index it can have an array of size 40. 例如,在第一个索引中,它可以具有大小为3的数组,而在第二个索引中,它可以具有大小为40的数组。

I want to know if you can have an array as elements because 2 dimensional arrays doesn't allow for the columns or rows to be able to have more than one dimesnion. 我想知道是否可以将数组作为元素,因为二维数组不允许列或行具有多个dimesnion。

Yes, that's what a "multi-dimensional" array in Java really is. 是的,这才是Java中的“多维”数组。 It's an array of array references. 这是一个数组引用数组。 Some of those references can be null, and the others can be of different sizes. 这些引用中的一些可以为null,其他的可以具有不同的大小。

For example: 例如:

int[][] array = new int[3][];
array[0] = new int[3];
array[1] = new int[40];
array[2] = null;

Yes, absolutely! 是的,一点没错! The feature you're looking for is called "jagged arrays", and it works exactly the way you expect. 您要查找的功能称为“锯齿状数组”,它的功能完全符合您的期望。

int arr[][] = new int[3][];   
arr[0] = new int[3];     
arr[1] = new int[40]; 
arr[2] = new int[12]; 

Source: http://way2java.com/arrays/jagged-arrays-varying-column-size-arrays/ 来源: http//way2java.com/arrays/jagged-arrays-varying-column-size-arrays/

How about this 这个怎么样

Object[][] arrays = new Object[5][];

then initialize your sub arrays with the sizes you want. 然后使用所需大小初始化子数组。 Something like: 就像是:

arrays[0] = new Object[3];
arrays[1] = new Object[5];

and so on 等等

Yes, you can, in most languages. 是的,您可以使用大多数语言。 C, C++ and Java for sure. 当然可以使用C,C ++和Java。

For some languages, such as C or C++, the size of the arrays that are elements are not important. 对于某些语言,例如C或C ++,作为元素的数组的大小并不重要。

Which language are you referring to? 您指的是哪种语言?

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

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