10

How do you create an array of arrays in C#? I have read about creating jagged arrays but I'm not sure if thats the best way of going about it. I was wanting to achieve something like this:

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

Then I can access it like myArray.myArray2[0];

I know that code won't work but just as an example to explain what I mean.

Thanks.

1
  • That doesn't make much sense. What are you trying to do? Commented Jun 19, 2011 at 16:30

6 Answers 6

16

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());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

If you wish to initialise a multidimensional array: int[][] myArrayOfIntArrays = { new int[] { 0, 1, 2 }, new int[] { 3, 4, 5 } };
8

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

Comments

4

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

Comments

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

4 Comments

What to do if you don't know the array limit? like a DB query?
@AlokRajasukumaran you won't be dealing with arrays to begin with so I don't really understand your question.
Ok, am working with Cassandra, and i got an object of data. i got a list data type, need to know how to use it.
@AlokRajasukumaran You seem to have a question, well then, ask a question; comments are not the tool you should be using.
0

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

Comments

0

I wanted to do the same thing to get a list of strings that an enum represented. Here is a possible solution:

     public enum Group
     {
          All = 0,
          Artists = 1,
          Builders = 2
      }

      private static readonly string[] _rolesAll = { "Brett", "Jeff", "Virgil", "Danielle" };
      private static readonly string[] _rolesArtists = { "Brett", "Danielle" };
      private static readonly string[] _rolesBuilders = { "Jeff" };

      private static readonly SortedList<Group, string[]> _sortedGroupToRoles = new SortedList<Group, string[]> { { Group.All, _rolesAll }, { Group.Artists, _rolesArtists}, { Group.Builders, _rolesBuilders } };

then to use it:

      _sortedGroupsToRoles.TryGetValue(Group.Artists, out string[] roles);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.