Array of Arrays

淺唱寂寞╮ 提交于 2019-12-03 16:08:39

Simple example of array of arrays or 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 test 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());
    }
}

you need a jagged array, that 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:

Before you can use jaggedArray, its elements must be initialized.

in your case you could wrap the array in another class but that seems highly redundant imho

You can use List of List. List - it is just dynamic array:

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

The way to do this is through jagged arrays:

string[][] myArray;

Your way doesnt really make sense:

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

It should be at least (if possible)

myArray[1].myArray2[20];

This is clearly worse than the standard way you would do things with jagged arrays: myArray[1][20];

I cant understand your real aim. But as option you can use dynamic type to create dynamic dictionary as decribed here (see examples section).
But its more likely that you need multidimentional array as described in others answers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!