Array of arrays

How do you create an array of arrays in C #? I read about creating gear arrays, but I'm not sure if this is the best way to get around this. I wanted to achieve something like this:

string[] myArray = {string[] myArray2, string[] myArray3}

I can then access it as myArray.myArray2[0];

I know that the code will not work, but as an example, to explain what I mean.

Thank.

+5
source share
5 answers

A simple example of an array of arrays or a multidimensional array is as follows:

int[] a1 = { 1, 2, 3 };
int[] a2 = { 4, 5, 6 };
int[] a3 = { 7, 8, 9, 10, 11 };
int[] a4 = { 50, 58, 90, 91 };

int[][] arr = {a1, a2, a3, a4};

To check the array:

for (int i = 0; i < arr.Length; i++)
{
    for (int j = 0; j < arr[i].Length; j++)
    {
        Console.WriteLine("\t" +  arr[i][j].ToString());
    }
}
+10
source

you need a jagged array , this is the best solution here

int[][] j = new int[][] 

or

string[][] jaggedArray = new string[3][];

jaggedArray[0] = new string[5];
jaggedArray[1] = new string[4];
jaggedArray[2] = new string[2]

then

jaggedArray[0][3] = "An Apple"
jaggedArray[2][1] = "A Banana"

etc...

Note:

jaggedArray, .

, imho

+5

List of List. - :

        var list = new List<List<int>>();
        list.Add(new List<int>());
        list[0].Add(1);
        Console.WriteLine(list[0][0]);
+3

:

string[][] myArray;

:

myArray.myArray2[20] // what element is the first array pointing to?

( )

myArray[1].myArray2[20];

, : myArray[1][20];

0

. dynamic , (. examples). < > , , .

0

All Articles