C # - multidimensional internal arrays

How do you declare a "deep" array in C #?

I would like to have an int array, for example: [1, 4, 5, 6, [3, 5, 6, 7, 9], 1, 4, 234, 2, 1,2,4,6,67, [1 , 2,4,44,56,7]]

I have done this before but cannot remember the correct syntax. But it was like what is written below: Int32 [] MyDeepArray = new Int32 [] = {3, 2, 1, 5, {1, 3, 4, 5}, 1, 4, 5};

And how to do it right? .. How to check that an array is an array?

Thanks!

+4
source share
4 answers

I believe the term you are looking for is a jagged array .

This can be done as follows:

int[][] jaggedArray2 = new int[][] { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22} }; 

And you can iterate over them like this:

 for(int i = 0; i < jaggedArray2.Length; i++) for(int j = 0; j < jaggedArray2[i].Length; j++) { //do something here. } 
+7
source

Int32 [] [] allows you to declare a two-dimensional array, where not all sizes must be the same. So, for example, you might have the following:

 [ [2,3,4,5] [5] [1,2,3,4,5,6,7,8] [3,5] [4] ] 

An alternative is Int32 [,], where the dimensions should always be the same.

I'm not sure what you mean by "how to verify that an array is an array".

+1
source

Here is some good documentation on using C # arrays. There is information about iteration using foreach and other methods.

http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

+1
source

Regarding "how to verify that an array is an array": C # is strongly typed. everything declared as an array must be an array. what you are looking for is an array, not integers, but arrays of integers. therefore, each element in your external array is a strongly typed array of integers. these are not integers and arrays of integers, all are confused. the closest thing you get is int arrays and int-array-contains-only-one-int. given that you can always iterate over them, despite the fact that they are all arrays, you can consider them as arrays, and those that contain only one element will only go into iteration once.

if you want to explicitly check because you handle int-array-only-one-int differently, you can check their .Length value.

0
source

All Articles